-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (123 loc) · 4.18 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict'
/**
* salak
*
* 创 建 者:wengeek <wenwei897684475@gmail.com>
* 创建时间:2017-12-20
*/
const Koa = require('koa')
const assert = require('assert')
const fs = require('fs')
const http = require('http')
const SalakCoreLoader = require('./lib/salakLoader')
const BaseContext = require('./lib/context')
const Ready = require('./util/ready')
const INIT_READY = Symbol('salakCore#initReady')
const LOAD_MIDDLEWARES = Symbol('salakCore#loadMiddlewares')
const DEFAULTS = Symbol('salakCore#defaults')
class SalakCore extends Koa {
constructor ({ baseDir = process.cwd(), opts = {
root: 'common',
readyTimeout: 120000
}}) {
assert(fs.existsSync(baseDir), `Directory ${baseDir} not exists`)
assert(fs.statSync(baseDir).isDirectory(), `Directory ${baseDir} is not a directory`)
super()
this.baseDir = baseDir
this.BaseContext = BaseContext
this.root = (opts && opts.root) || 'common' // common目录名字,通过opts.root 可修改目录名
this.loader = new SalakCoreLoader({
app: this,
baseDir: baseDir,
env: this.env
})
this.readyTimeout = (opts && opts.readyTimeout) || 120000
this.readyInstance = Ready.getInstance({
timeout: this.readyTimeout
})
this[INIT_READY]()
}
beforeStart (scope) {
if (typeof scope === 'function') {
this.readyInstance.ready(scope)
}
}
[INIT_READY] () {
this.rootConfig = this.loader.getConfig()
this.mode = this.rootConfig.bootstraps && this.rootConfig.bootstraps.length ? 'multiple' : 'single' // 两种模式,单模块应用single 或者 多模块 multiple,根据bootstraps识别
this.helper = this.loader.loadHelper()
const { modules, configs } = this.loader.getModuleConfigs(this.rootConfig)
this.modules = modules
this.configs = configs
this.logger = this.loader.loadLogger(this.rootConfig.logger)
this.services = this.loader.loadDir(this.modules, 'service')
this.buildInMiddlewares = SalakCore[DEFAULTS].buildInMiddlewares || {} // 框架自带的中间件
this.buildInMiddlewaresOrder = SalakCore[DEFAULTS].buildInMiddlewaresOrder || []// 框架自带中间件默认执行顺序
this.readyInstance.on('timeout', () => {
this.emit('ready_timeout')
this.logger.app.error(`[salak-core:ready_timeout] ${this.readyTimeout / 1000 || 10000} seconds later was still unable to start. `)
}).on('ready', () => {
this.emit('ready')
this.logger.app.info(`[salak-core:ready] salak was ready.`)
})
// for beforeStart
process.nextTick(() => {
this.readyInstance.ready(() => {
this.middlewares = this.loader.loadDir(this.modules, 'middleware')
// 加载路由器
const { router, routesDefinitions } = this.loader.loadController(this.modules) || {}
this.router = router
// 可用于swagger
this.routesDefinitions = routesDefinitions
// 加载中间件
this[LOAD_MIDDLEWARES]()
// 加载定时任务
this.loader.loadSchedules()
})
this.readyInstance.ready(true)
})
}
/**
* 创建匿名context,用于调用service
*/
createAnonymousContext () {
const request = {
headers: {
'x-forwarded-for': '127.0.0.1'
},
query: {},
querystring: '',
host: '127.0.0.1',
hostname: '127.0.0.1',
protocol: 'http',
secure: 'false',
method: 'GET',
url: '/',
path: '/',
socket: {
remoteAddress: '127.0.0.1',
remotePort: 7001
}
}
const response = new http.ServerResponse(request)
return this.createContext(request, response)
}
[LOAD_MIDDLEWARES] () {
const middlewares = this.loader.loadMiddlewares(this.rootConfig.middleware || this.buildInMiddlewaresOrder, this.root)
if (middlewares) {
this.use(middlewares)
}
this.use(this.router.routes())
}
}
SalakCore[DEFAULTS] = {
buildInMiddlewares: {},
buildInMiddlewaresOrder: []
}
SalakCore.Controller = require('./lib/controller')
SalakCore.Service = BaseContext
SalakCore.BaseContext = BaseContext
SalakCore.Schedule = BaseContext
SalakCore.defaults = DEFAULTS
SalakCore.Joi = require('salak-router').Joi
module.exports = SalakCore