Skip to content

Commit

Permalink
Merge pull request #69 from MichealWayne/feat-utils-npm
Browse files Browse the repository at this point in the history
feat add koa2 template
  • Loading branch information
MichealWayne authored May 21, 2024
2 parents 31e7e41 + 49d54ed commit a67f253
Show file tree
Hide file tree
Showing 151 changed files with 535 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/doc-build-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Checkout
uses: actions/checkout@master

- run: mkdir -p ./docs
- run: rm -rf ./docs && mkdir -p ./docs
- name: Download the new documents artifact
uses: actions/download-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 更新信息

- 2024.05.14:ai utils增加单测prompt;主页链接调整
- 2024.05.14:ai utils增加单测prompt;主页链接调整;整合模版(增加koa2模版)
- 2024.04.27:web utils资源加载方法、ua环境判断优化
- 2024.04.07:utils 增加 env环境判断、web utils 增加资源加载相关方法
- 2024.03.10:修改 utils & web util functions(部分方法类型声明及参数处理优化, Storage)
Expand Down
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,21 @@

> todo: 小程序封装,构建封装。
#### 2.2 fe-templates 一些前端项目模版

- [webpack](./fe-templates/webpack/)
- [webpack + ts](./fe-templates/webpack%2Bts/)
- [vite + vue3](./fe-templates/vite-vue3+ts/)
- [vue2](./fe-templates/vue/)
- [vite + react 17](./fe-templates/vite-react+ts/)
- [react16](./fe-templates/react/)
#### 2.2 project-templates 一些前后端项目模版

`frontend` 前端项目模版

- [webpack](./project-templates/frontend/webpack/)
- [webpack + ts](./project-templates/frontend/webpack%2Bts/)
- [vite + vue3](./project-templates/frontend/vite-vue3+ts/)
- [vue2](./project-templates/frontend/vue/)
- [vite + react 17](./project-templates/frontend/vite-react+ts/)
- [react16](./project-templates/frontend/react/)

`backend` 后端项目模版

- [koa2](./project-templates/backend/koa2/)


#### 2.3 ~~styles基础样式库~~

Expand Down
23 changes: 23 additions & 0 deletions project-templates/backend/koa2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Koa2 template

nodejs 服务模版——Koa2

## 项目运行

启动:

```sh
npm run start
```

构建:

```sh
npm run build
```

生产构建:

```sh
npm run build:prod
```
43 changes: 43 additions & 0 deletions project-templates/backend/koa2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "koa2-template",
"version": "1.0.0",
"port": "5000",
"author": "Wayne",
"description": "koa2 nodejs server template.",
"main": "dist/index.js",
"scripts": {
"start": "tsc && node dist/",
"dev": "tsc && set NODE_ENV=development && node dist/",
"build": "rm -rf dist/* && tsc",
"build:prod": "tsc && rm -rf node_modules && npm install --production",
"install:prod": "npm install --production",
"docker:start": "npm install && node dist/"
},
"keywords": [
"koa2",
"nodejs"
],
"license": "ISC",
"devDependencies": {
"@types/koa": "^2.11.6",
"@types/koa-router": "^7.4.1",
"@types/koa2-cors": "^2.0.2",
"@types/log4js": "^2.3.5",
"@types/node": "^12.19.3",
"@types/uuid": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"eslint-config-sonarjs": "^1.0.19",
"eslint-plugin-typescript": "^0.14.0",
"typescript": "^4.0.0"
},
"dependencies": {
"axios": "^1.2.0",
"koa": "^2.13.0",
"koa-body": "^4.2.0",
"koa-router": "^9.4.0",
"koa2-cors": "^2.0.6",
"log4js": "^6.0.0",
"uuid": "^9.0.0"
}
}
83 changes: 83 additions & 0 deletions project-templates/backend/koa2/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2024-05-17 09:55:55
*/
import { join } from 'path';

const pkg = require(join(__dirname, '../package.json'));

export const LOG_INFO = {
// 日志配置
appenders: {
console: {
type: 'console',
},
trace: {
type: 'dateFile',
filename: './logs/access',
pattern: '.yyyy.log',
alwaysIncludePattern: true,
'maxLogSize ': 204800,
},
http: {
type: 'logLevelFilter',
appender: 'trace',
level: 'trace',
maxLevel: 'trace',
},
info: {
type: 'dateFile',
filename: './logs/info',
encoding: 'utf-8',
pattern: '.yyyy.log',
maxLogSize: 204800,
alwaysIncludePattern: true,
layout: {
type: 'pattern',
pattern: '[%d{ISO8601}][%5p %z %c] %m',
},
compress: true,
},
maxInfo: {
type: 'logLevelFilter',
appender: 'info',
level: 'debug',
maxLevel: 'error',
},
error: {
type: 'dateFile',
filename: './logs/error',
pattern: '.yyyy.log',
maxLogSize: 204800,
encoding: 'utf-8',
alwaysIncludePattern: true,
layout: {
type: 'pattern',
pattern: '[%d{ISO8601}][%5p %z %c] %m',
},
compress: true,
},
minError: {
type: 'logLevelFilter',
appender: 'error',
level: 'error',
},
},
categories: {
default: {
appenders: ['console', 'http', 'maxInfo', 'minError'],
level: 'all',
},
},
};

export const isDev = process.env.NODE_ENV === 'development';
export default {
port: pkg.port,
isDev,
file: {
multipart: true,
maxFileSize: 500 * 1024 * 1024,
},
};
20 changes: 20 additions & 0 deletions project-templates/backend/koa2/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @module constant
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2024-05-17 09:48:46
*/
export const SERVER_NAME = 'koa2-template';

export const LOG_METHODS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];

export const CODE_NAME = 'status_code';
export const MESSAGE_NAME = 'status_msg';
export const INFODATA_NAME = 'data';

export const SERVICE_PROTOCOL = 'http:';

export const SUCCESS_CODE = 0;
export const ERROR_CODE = 9999;

export const TIMEOUT = 20 * 1000;
39 changes: 39 additions & 0 deletions project-templates/backend/koa2/src/controllers/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2024-05-17 09:49:50
*/
import Koa from 'koa';

import { nextInstance } from '../types';

import { SERVER_NAME } from '../constant';

import logger from './logger';

/**
* hello world(for healthy test)
*/
export const HelloWorld = async (ctx: Koa.BaseContext) => {
ctx.body = `hello ${SERVER_NAME}.`;
};

export const GetTest = async (ctx: Koa.DefaultContext, next: nextInstance) => {
ctx.body = {
result: 'get',
name: ctx.params.name,
param: ctx.query,
};
if (ctx.params.prompt) logger.info(`${JSON.stringify(ctx.params)}`);
next();
};

export const PostTest = async (ctx: Koa.DefaultContext, next: nextInstance) => {
ctx.body = {
result: 'post',
name: ctx.params.name,
param: ctx.request.body,
};

next();
};
29 changes: 29 additions & 0 deletions project-templates/backend/koa2/src/controllers/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2023-06-25 13:37:59
*/
import log4js from 'log4js';

import { LOG_METHODS } from '../constant';
import { globalLog } from '../types';
import { LOG_INFO, isDev } from '../config';

log4js.configure(LOG_INFO);
const _logger: any = log4js.getLogger('index');

const _logHoc =
(method: string) =>
(info: string, ...args: string[]) => {
if (isDev) {
console.log(info, ...args);
}
_logger[method](info, ...args);
};

const logger: globalLog = {};
LOG_METHODS.forEach((method: string) => {
logger[method] = _logHoc(method);
});

export default logger;
29 changes: 29 additions & 0 deletions project-templates/backend/koa2/src/controllers/setRes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2023-05-19 15:51:42
*/
import Koa from 'koa';
import { CODE_NAME, MESSAGE_NAME, SUCCESS_CODE, ERROR_CODE } from '../constant';

export function sendSuccess(res: Koa.DefaultContext, extobj = {}) {
const resObj = {
[CODE_NAME]: SUCCESS_CODE,
[MESSAGE_NAME]: 'success',
...extobj,
};

res.body = resObj;
return resObj;
}

export function sendFail(res: Koa.DefaultContext, extobj = {}) {
const resObj = {
[CODE_NAME]: ERROR_CODE,
[MESSAGE_NAME]: 'fail',
...extobj,
};

res.body = resObj;
return resObj;
}
35 changes: 35 additions & 0 deletions project-templates/backend/koa2/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @author Wayne
* @Date 2023-02-15 15:18:59
* @LastEditTime 2024-05-17 09:51:06
*/
import Koa from 'koa';
// import cors from 'koa2-cors'; 跨域处理,只有必要时开启

import { SERVER_NAME } from './constant';

import config from './config';
import logger from './controllers/logger';

import bodyHandler from './middlewares/bodyHandler';
import errorHandler from './middlewares/errorHandler';

import router from './routes';

const app = new Koa();

app.use(bodyHandler);
app.use(errorHandler);
// app.use(cors());

app.use(router.routes()).use(router.allowedMethods());

app.listen(config.port, () => {
logger.info(new Date().toLocaleString());
logger.info(`Service Loaded(${SERVER_NAME})`);
logger.info('Port', config.port);
});

app.on('error', (err, ctx) => {
logger.error('server error', err, ctx);
});
17 changes: 17 additions & 0 deletions project-templates/backend/koa2/src/middlewares/bodyHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @author Wayne
* @Date 2023-05-09 20:38:53
* @LastEditTime 2023-12-29 15:02:17
*/
import koaBody from 'koa-body';

import config from '../config';

const bodyHandler = koaBody({
multipart: config.file.multipart,
formidable: {
maxFileSize: config.file.maxFileSize,
},
});

export default bodyHandler;
Loading

0 comments on commit a67f253

Please sign in to comment.