/
index.js
60 lines (52 loc) · 1.67 KB
/
index.js
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
/**
* Powered by zydsoft™
* 2021-3-5
* zz
*/
const Koa = require("koa")
const { initController, initService, initConfig, initSchedule, initModel, initPlugin } = require("./loader")
const version = require('./package.json').version
class Zyd {
constructor(conf) {
console.log(`\x1B[33m
┌──────────────────────┐
│ Powered by zydsoft® │
│ zyd-server-framework │
│ v${version} │
└──────────────────────┘
\x1B[0m`)
this.$app = new Koa()
//生命周期函数 - init前 zz 2021-4-23
if (conf && conf.beforeInit) conf.beforeInit(this.$app)
this.$global = {} //定义全局变量
if (conf && conf.baseUrl) { // 复制全局变量得基础路径
this.$global.baseUrl = conf.baseUrl
}
this.$global.port = 3000
//init
//执行配置文件
this.$config = initConfig(this) //返回config配置
this.$plugin = initPlugin(this)
initSchedule(this)
this.$service = initService(this)
this.$model = initModel(this)
this.$controller = initController(this)
//生命周期函数 - init后 zz 2021-4-23
if (conf && conf.afterInit) conf.afterInit(this.$app)
this.$app.use(this.$controller.routes())
}
start(port = this.$global.port, callBack = () => {
console.log("start on port:" + port)
}) {
this.$global.port = port
this.$app.listen(port, () => {
callBack && callBack()
console.log(`\x1B[1m\x1B[32m
┌───────────────┐
│ start success │
└───────────────┘
\x1B[0m\x1B[0m`)
})
}
}
module.exports = Zyd