Skip to content

Commit

Permalink
add eggjs template (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-ivy authored May 27, 2023
1 parent cdee7e0 commit d5615ef
Show file tree
Hide file tree
Showing 32 changed files with 711 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = {
write: true,
plugin: 'autod-egg',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
'coverage',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'autod',
'autod-egg',
'egg-bin',
'tslib',
'typescript',
],
keep: [
],
semver: [
],
test: 'scripts',
};
21 changes: 21 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js
!.autod.conf.js

app/**/*.js
test/
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
.git
2 changes: 2 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.d.ts
node_modules/
6 changes: 6 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "eslint-config-egg/typescript",
"parserOptions": {
"project": "./tsconfig.json"
}
}
20 changes: 20 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js
!.autod.conf.js

app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
12 changes: 12 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

language: node_js
node_js:
- '8'
before_install:
- npm i npminstall@5 -g
install:
- npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
20 changes: 20 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:12.6.0-alpine

# 创建app目录
RUN mkdir -p /usr/src/app

# 设置容器内工作目录
WORKDIR /usr/src/app

COPY package.json /usr/src/app/

RUN yarn

# 拷贝所有源代码到工作目录
COPY . /usr/src/app

# 暴露容器端口
EXPOSE 7001

# 启动node应用
ENTRYPOINT ["npm", "run"]
58 changes: 58 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# egg cli 服务

[建议读官方文档](https://eggjs.org/zh-cn/intro/quickstart.html)

>推荐使用docker启动egg服务
## 开发测试

step:

1. ```yarn```

2. ```npm run dev```

## 线上

```docker run --name test-egg -p 9999:7001 --net egg-mysql-net tgc-egg:v1.4 -d start```

接口路径:host:9999

## 关于连接mysql数据库注意事项

docker 服务启动后创建一个新的网络模式egg-mysql-net(bridger)更好的管理namsespace
```
docker network create egg-mysql-net
```


建议直接使用mysql官方的docker镜像启动服务
```
docker run --name test-mysql -p 3333:3306 --net egg-mysql-net -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
```
进入到mysql容器内:

```docker exec -it test-mysql sh```

登录mysql:

```mysql -u root -p 123456```


将/app/databases 中的sql运行一遍即可

或者用主机连接容器内部的数据库(推荐使用Navicat fo MySQL)

```
->新建连接 -> 常规
连接名:xxxx
主机名:localhost
端口:3333
用户名:root
密码:123456
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Controller } from 'egg';

export default class EmployeeController extends Controller {
public async getEmployee() {
const { ctx } = this;
const { query } = ctx;
const { pageIndex = '1', pageSize = '20', searchInfo = '{}' } = query;
const [ offset, limit ] = ctx.helper.getPagination(pageIndex, pageSize);

try {
const where = ctx.helper.getWhere(JSON.parse(searchInfo));
let { sortField, sortType } = query;

[sortField, sortType] = ctx.helper.getSortFieldAndType(
sortField,
sortType,
);
const options = {
where,
limit,
offset,
order: [[sortField, sortType]],
raw: true,
};

this.logger.info('[ controller | employee ] getEmployee : 进入getEmployee方法');
const result = await ctx.service.employee.getEmployee(options);
ctx.helper.commonJson(
ctx,
{
data: result.rows,
total: result.count,
},
200,
);
} catch (error) {
console.log('error', error)
// ctx.helper.exceptionHandling(ctx, error);
ctx.helper.commonJson(ctx, {}, 500, 'CM087');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 关于数据库及表生成注意事项

**初始化数据库的时候建议直接使用sql语句**

初始化新表时最好是将表对应的sql写到每个文件中
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE buryinfo(
`_id` INT NOT NULL AUTO_INCREMENT COMMENT '自增id',
`type_id` VARCHAR(50) NOT NULL COMMENT '埋点ID',
`business_desc` VARCHAR(255) NOT NULL COMMENT '业务描述',
`parameter` VARCHAR(255) COMMENT '埋点自定义参数',
`user_email` VARCHAR(50) COMMENT '注册人邮箱',
`user_phone` VARCHAR(20) COMMENT '注册人电话',
`user_name` VARCHAR(50) NOT NULL COMMENT '注册人姓名',
`is_registered` TINYINT(3) DEFAULT 1 COMMENT '是否在鲁班上注册过',
`disable` TINYINT(3) DEFAULT 1 COMMENT '是否启用埋点',
`platform` VARCHAR(50) NOT NULL COMMENT '平台',
`category` VARCHAR(255) NOT NULL COMMENT '目录',
`created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (_id),
INDEX locktype (platform, category)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE DATABASE tiny_server_test;

USE tiny_server_test;
60 changes: 60 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/app/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Op } from 'sequelize';
import BaseUtils from '../utils/base-utils';

const DEFAULT_SORT_FIELD = 'create_time';
const SORT_ASC = 'asc';
const SORT_DESC = 'desc';

export default {
exceptionHandling(ctx, error, message = '') {
const errMsg = BaseUtils.getErrorMessage(error);

if (message) {
ctx.logger.error(message, errMsg);
} else {
ctx.logger.error(errMsg);
}
},

commonJson(
ctx,
data?: Record<string, any>,
httpStatus?: number,
errCode?: string,
errMsg?: string,
) {
const responseBody = {
code: errCode ? errCode : '0',
errMsg: errMsg || (errCode ? ctx.__(errCode) : ''),
data: data ? data : {},
};
ctx.status = httpStatus ? httpStatus : 200;
ctx.body = responseBody;
},

getPagination(pageIndex: string = '1', pageSize: string = '20') {
let no = Number(pageIndex);
let size = Number(pageSize);
no = isNaN(no) || no < 1 ? 1 : no;
size = isNaN(size) || size < 0 ? 20 : size;
return [ (no - 1) * size, size ];
},

getSortFieldAndType(sortField: string, sortType: string = 'desc') {
return [ (sortField || DEFAULT_SORT_FIELD), (sortType === SORT_ASC)? SORT_ASC : SORT_DESC ];
},

getWhere(searchInfo: { [prop: string]: string } = {}) {
const where = {};

Object.keys(searchInfo).map(key => {
const value = searchInfo[key];

where[key] = {
[Op.like]: `%${value}%`,
}
})

return where;
}
};
76 changes: 76 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/app/model/employee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* indent size: 4 */

module.exports = (app: any) => {
const DataTypes = app.Sequelize;

const Model = app.model.define(
'employee',
{
id: {
type: DataTypes.INTEGER(16),
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING(20),
allowNull: false,
},
employee_no: {
type: DataTypes.STRING(50),
allowNull: false,
},
department: {
type: DataTypes.STRING(50),
allowNull: false,
},
department_level: {
type: DataTypes.STRING(50),
allowNull: false,
},
status: {
type: DataTypes.STRING(10),
allowNull: false,
},
workbench_name: {
type: DataTypes.STRING(50),
allowNull: false,
},
project: {
type: DataTypes.STRING(50),
allowNull: false,
},
type: {
type: DataTypes.STRING(50),
allowNull: false,
},
address: {
type: DataTypes.STRING(50),
allowNull: false,
},
roles: {
type: DataTypes.STRING(50),
allowNull: false,
},
last_update_user: {
type: DataTypes.STRING(50),
allowNull: false,
},
create_time: {
type: DataTypes.TIME,
allowNull: true,
defaultValue: DataTypes.literal('CURRENT_TIMESTAMP'),
},
},
{
tableName: 'employee',
underscored: false,
freezeTableName: true,
omitNull: false,
timestamps: false,
paranoid: false,
},
);

return Model;
};
11 changes: 11 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Application } from 'egg';

export default (app: Application) => {
const { controller, router } = app;

// todo: init database connect
// router.post('/v1/database/init');

router.get('/v1/employee/getEmployee', controller.employee.getEmployee);

};
Loading

0 comments on commit d5615ef

Please sign in to comment.