-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.js
More file actions
128 lines (111 loc) · 3.39 KB
/
install.js
File metadata and controls
128 lines (111 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const libs = require('./libs')
const modelss = require('./models')
const services = require('./services')
const config = require('./configs')
const middlewares = require('./middlewares')
const koaSwagger = require('koa2-swagger-ui');
const apis = require('./apis')
const Router = require('koa-router');
const multer = require('@koa/multer');
const path = require('path');
// libs
async function libsIntall(list) {
libs.install(list);
}
// config
async function configInstall() {
// console.log('process.env ', process.env);
let env = process.env.NODE_ENV;
let SERVER_TYPE = process.env.SERVER_TYPE;
G.env = env;
G.SERVER_TYPE = SERVER_TYPE
G.config = await config.install(env);
}
// router
async function installRouter() {
let router = new Router();
G.router = router;
G.app.use(G.router.routes());
}
// load db
async function installDb() {
G.db = {};
G.models = { main: {} , othersDbModels: {} }
// mysql
let configlist = Object.keys(G.config.mysqlConfig);
for (let i = 0; i < configlist.length; i++) {
let cfgKey = configlist[i]
let cfg = G.config.mysqlConfig[cfgKey]
let mysqlTool = await G.libs.dbTool.SequelizeIntall(cfg, cfgKey);
let models = await modelss.install(mysqlTool.getDB(), cfgKey);
if (cfgKey == 'main') {
G.models.main = models
} else {
G.models.othersDbModels[cfgKey] = models
}
await mysqlTool.assignModels(models);
}
// redis
await G.libs.dbTool.RedisIntall(G.config.redisConfig);
}
// load middle
async function installMiddles(middles = []) {
G.middlewares = {};
G.middlewares = await middlewares.install();
for (let i = 0; i < middles.length; i++) {
if (middles[i]) {
G.router.use(G.middlewares[middles[i]]());
} else {
throw new Error(middles[i] + ' middle not exist');
}
}
}
// services load
async function installServices(app) {
let context = { models: G.models.main, othersDbModels: G.models.othersDbModels };
let all = services.install(context);
context.services = all;
G.services = all;
return all;
}
// upload file
async function fileUpload(app) {
let uploadTool = multer({ dest: path.join(__dirname, './upload/') });
G.uploadTool = uploadTool;
}
// load swagger
async function swagger(app) {
G.router.get('/swagger', koaSwagger({ routePrefix: false, swaggerOptions: { spec: G.libs.swagger.getJson() } }));
}
module.exports.installAll = async function installAll(app) {
G.app = app;
await configInstall();
await libsIntall(['dbTool', 'token', 'validator', 'argfilter', 'swagger']);
await installRouter();
G.router.use(async function (ctx, next) {
ctx.models = G.models;
ctx.services = G.services;
ctx.success = function(data, msg = "ok") {
return ctx.body = {
code: "200",
msg: msg,
data: data
}
}
ctx.fail = function(msg = "error", code = "400", data = {}) {
return ctx.body = {
code: code,
msg: msg,
data: data
}
}
await next();
});
await installDb();
await installMiddles(['params']);
await fileUpload(app);
await apis.install();
await installServices(app);
await swagger(app)
console.log('installAll end');
};