Shaku 开源项目使用文档

Shaku 开源项目使用文档

shakuShaku helps you write better technical articles with code annotation .etc项目地址:https://gitcode.com/gh_mirrors/sha/shaku

1. 项目的目录结构及介绍

shaku/
├── src/
│   ├── core/
│   │   ├── index.ts
│   │   ├── service.ts
│   │   └── logger.ts
│   ├── app/
│   │   ├── index.ts
│   │   ├── controller.ts
│   │   └── middleware.ts
│   └── config/
│       ├── default.json
│       └── production.json
├── tests/
│   ├── unit/
│   │   └── example.test.ts
│   └── integration/
│       └── example.test.ts
├── package.json
├── tsconfig.json
└── README.md
  • src/: 源代码目录
    • core/: 核心模块,包含服务和日志功能
    • app/: 应用模块,包含控制器和中间件
    • config/: 配置文件目录
  • tests/: 测试目录
    • unit/: 单元测试
    • integration/: 集成测试
  • package.json: 项目依赖和脚本
  • tsconfig.json: TypeScript 配置文件
  • README.md: 项目说明文档

2. 项目的启动文件介绍

项目的启动文件位于 src/app/index.ts。该文件主要负责初始化应用实例、加载配置、注册中间件和启动服务器。

import express from 'express';
import { config } from '../config';
import { registerMiddleware } from './middleware';
import { registerRoutes } from './controller';

const app = express();
const port = config.port || 3000;

registerMiddleware(app);
registerRoutes(app);

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

3. 项目的配置文件介绍

项目的配置文件位于 src/config/ 目录下,包含 default.jsonproduction.json 两个文件。

  • default.json: 默认配置文件,包含开发环境的配置。
  • production.json: 生产环境配置文件,会覆盖默认配置中的相应字段。

示例 default.json:

{
  "port": 3000,
  "logLevel": "debug",
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "mydb"
  }
}

示例 production.json:

{
  "port": 8080,
  "logLevel": "info",
  "database": {
    "host": "prod-db-server",
    "port": 5432,
    "name": "prod-db"
  }
}

配置文件通过 src/config/index.ts 文件加载和合并:

import * as path from 'path';
import * as fs from 'fs';
import * as _ from 'lodash';

const defaultConfigPath = path.resolve(__dirname, 'default.json');
const envConfigPath = path.resolve(__dirname, `${process.env.NODE_ENV || 'development'}.json`);

const defaultConfig = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf-8'));
const envConfig = fs.existsSync(envConfigPath) ? JSON.parse(fs.readFileSync(envConfigPath, 'utf-8')) : {};

export const config = _.merge(defaultConfig, envConfig);

通过这种方式,可以根据不同的环境加载相应的配置。

shakuShaku helps you write better technical articles with code annotation .etc项目地址:https://gitcode.com/gh_mirrors/sha/shaku

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀灏其Prudent

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值