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 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
- console.log('user id ---> ', appName, '--->', user.id, user.name)
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 routerName = this.config.includes[0].routerName
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/${routerName}/setting/findOne`
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
- if (this.config.includes.length === 0) throw new Error('请填入项目')
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 < this.config.includes.length; i++) {
311
- const routerName = this.config.includes[i].routerName
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: `/${routerName}`,
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
- is_available: false,
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
- this.initTask(appName)
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 routerName = router.opts.prefix.substring(1)
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 routerName = router.opts.prefix.substring(1)
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
@@ -5,6 +5,10 @@ module.exports = {
5
5
  static: {
6
6
  dir: 'public',
7
7
  },
8
+ log: {
9
+ request: true,
10
+ },
11
+ blackList: [],
8
12
  task: {
9
13
  dir: 'task',
10
14
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "8.0.5",
3
+ "version": "8.1.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {