q-koa 8.0.5 → 8.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/app.js +29 -34
- package/core/config.js +4 -0
- package/package.json +1 -1
package/core/app.js
CHANGED
|
@@ -70,16 +70,7 @@ class APP {
|
|
|
70
70
|
constructor(_config) {
|
|
71
71
|
const config = _.defaultsDeep(_config, defaultConfig)
|
|
72
72
|
const { port } = config
|
|
73
|
-
this.config =
|
|
74
|
-
...config,
|
|
75
|
-
includes: config.includes.map((item) => {
|
|
76
|
-
return typeof item === 'string'
|
|
77
|
-
? {
|
|
78
|
-
item,
|
|
79
|
-
}
|
|
80
|
-
: item
|
|
81
|
-
}),
|
|
82
|
-
}
|
|
73
|
+
this.config = config
|
|
83
74
|
this.port = port
|
|
84
75
|
this.app = new Koa()
|
|
85
76
|
this.server = null
|
|
@@ -142,6 +133,12 @@ class APP {
|
|
|
142
133
|
this.app.use(async (ctx, next) => {
|
|
143
134
|
const verify = util.promisify(jwt.verify)
|
|
144
135
|
const { url, host, ip, method } = ctx.request
|
|
136
|
+
if (this.config.blackList.includes(ip)) {
|
|
137
|
+
if (this.config.log.request) {
|
|
138
|
+
console.log('IP==>', ip)
|
|
139
|
+
}
|
|
140
|
+
return ctx.ERROR('error')
|
|
141
|
+
}
|
|
145
142
|
const appName = url.split('/')[1]
|
|
146
143
|
const { secret, whiteList, _whiteList, tokenKey, expiresIn } =
|
|
147
144
|
ctx.app[appName] && ctx.app[appName].appConfig
|
|
@@ -188,7 +185,7 @@ class APP {
|
|
|
188
185
|
ctx.cookies.get(`${appName}-${tokenKey}`) ||
|
|
189
186
|
ctx.query[`${appName}-${tokenKey}`]
|
|
190
187
|
|
|
191
|
-
if (url.length > 2) {
|
|
188
|
+
if (url.length > 2 && this.config.log.request) {
|
|
192
189
|
console.log(
|
|
193
190
|
moment().format('HH:mm:ss'),
|
|
194
191
|
url,
|
|
@@ -200,7 +197,9 @@ class APP {
|
|
|
200
197
|
try {
|
|
201
198
|
const { user } = await verify(token, secret)
|
|
202
199
|
if (user) {
|
|
203
|
-
|
|
200
|
+
if (this.config.log.request) {
|
|
201
|
+
console.log('user id ---> ', appName, '--->', user.id, user.name)
|
|
202
|
+
}
|
|
204
203
|
ctx.request[`${appName}-user`] = user
|
|
205
204
|
}
|
|
206
205
|
} catch (e) {
|
|
@@ -253,12 +252,11 @@ class APP {
|
|
|
253
252
|
// });
|
|
254
253
|
|
|
255
254
|
starter.on('loadAll', async () => {
|
|
256
|
-
const
|
|
257
|
-
const appName = this.config.includes[0].dirName
|
|
255
|
+
const appName = this.config.includes[0]
|
|
258
256
|
this.server = this.app.listen(this.port, () => {
|
|
259
257
|
console.log(`server is running at http://localhost:${this.port}`)
|
|
260
258
|
console.log(
|
|
261
|
-
`first app is http://localhost:3001/${
|
|
259
|
+
`first app is http://localhost:3001/${appName}/setting/findOne`
|
|
262
260
|
)
|
|
263
261
|
})
|
|
264
262
|
|
|
@@ -305,17 +303,20 @@ class APP {
|
|
|
305
303
|
}
|
|
306
304
|
|
|
307
305
|
async initApp() {
|
|
308
|
-
|
|
306
|
+
let dirList = await fsPromise.readdir(
|
|
307
|
+
path.resolve(__dirname, `${rootDirectory}/${APP_DIR}`)
|
|
308
|
+
)
|
|
309
|
+
dirList = dirList.filter((item) => this.config.includes.includes(item))
|
|
310
|
+
|
|
309
311
|
let dbFlag = false
|
|
310
|
-
for (let i = 0; i <
|
|
311
|
-
const
|
|
312
|
-
const appName = this.config.includes[i].dirName
|
|
312
|
+
for (let i = 0; i < dirList.length; i++) {
|
|
313
|
+
const appName = dirList[i]
|
|
313
314
|
|
|
314
315
|
console.log(
|
|
315
316
|
chalk.green(`==================== 启动 ${appName} ====================`)
|
|
316
317
|
)
|
|
317
318
|
const routerConfig = {
|
|
318
|
-
prefix: `/${
|
|
319
|
+
prefix: `/${appName}`,
|
|
319
320
|
}
|
|
320
321
|
const router = new Router(routerConfig)
|
|
321
322
|
Serve(
|
|
@@ -351,9 +352,9 @@ class APP {
|
|
|
351
352
|
}
|
|
352
353
|
|
|
353
354
|
// const proxyConfig = _.get(this.app[appName], 'appConfig.proxy');
|
|
354
|
-
const taskConfig = _.get(this.app[appName], 'appConfig.task', {
|
|
355
|
-
|
|
356
|
-
})
|
|
355
|
+
// const taskConfig = _.get(this.app[appName], 'appConfig.task', {
|
|
356
|
+
// is_available: false,
|
|
357
|
+
// })
|
|
357
358
|
// if (proxyConfig) {
|
|
358
359
|
// this.app.use(proxy(`/${appName}`, proxyConfig));
|
|
359
360
|
// }
|
|
@@ -382,9 +383,9 @@ class APP {
|
|
|
382
383
|
|
|
383
384
|
await this.initModel(appName)
|
|
384
385
|
// const models = Object.keys(this.app[appName].model).join(' / ')
|
|
385
|
-
if (taskConfig.is_available) {
|
|
386
|
-
|
|
387
|
-
}
|
|
386
|
+
// if (taskConfig.is_available) {
|
|
387
|
+
// this.initTask(appName)
|
|
388
|
+
// }
|
|
388
389
|
} catch (e) {
|
|
389
390
|
console.log(`${appName}数据库连接失败`)
|
|
390
391
|
console.log(e)
|
|
@@ -791,10 +792,7 @@ class APP {
|
|
|
791
792
|
|
|
792
793
|
initRouter(router) {
|
|
793
794
|
router.post('/:controller', async (ctx, next) => {
|
|
794
|
-
const
|
|
795
|
-
const appName = this.config.includes.find(
|
|
796
|
-
(i) => i.routerName === routerName
|
|
797
|
-
)
|
|
795
|
+
const appName = router.opts.prefix.substring(1)
|
|
798
796
|
const { app } = ctx
|
|
799
797
|
const { controller } = ctx.params
|
|
800
798
|
if (
|
|
@@ -806,10 +804,7 @@ class APP {
|
|
|
806
804
|
await next()
|
|
807
805
|
})
|
|
808
806
|
const handleRouter = async (ctx) => {
|
|
809
|
-
const
|
|
810
|
-
const appName = this.config.includes.find(
|
|
811
|
-
(i) => i.routerName === routerName
|
|
812
|
-
).dirName
|
|
807
|
+
const appName = router.opts.prefix.substring(1)
|
|
813
808
|
const { app } = ctx
|
|
814
809
|
|
|
815
810
|
const { controller, fn, sub } = ctx.params
|
package/core/config.js
CHANGED