优化架构,新增路由
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
module.exports = {
|
||||
path:"app/*/api/*.js"
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/**
|
||||
* 加载api路由
|
||||
* 加载app/api目录下的文件到路由
|
||||
*/
|
||||
const config = require("./config")
|
||||
module.exports = async (app) => {
|
||||
let list = await app.load(config.path)
|
||||
list.forEach(item => {
|
||||
app.use(async (ctx, next) => {
|
||||
return await item.res.fun(ctx, next)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
module.exports = {
|
||||
success: (body, msg, status) => {
|
||||
return {
|
||||
res : body || null,
|
||||
status: status || 200,
|
||||
message : msg || "success"
|
||||
}
|
||||
},
|
||||
error : (body, msg, status) => {
|
||||
return {
|
||||
res : body || null,
|
||||
status: status || 400,
|
||||
message : msg || "error"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
module.exports = {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* 封装api返回结果
|
||||
*/
|
||||
const config = require("./config")
|
||||
const body = require("./body")
|
||||
|
||||
module.exports = async (app) => {
|
||||
app.res = body
|
||||
}
|
||||
+10
-4
@@ -3,7 +3,7 @@
|
||||
* 加载schedule文件夹下的任务
|
||||
* https://blog.pincman.com/archives/18/
|
||||
* https://docs.bullmq.io
|
||||
*
|
||||
* https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md
|
||||
*/
|
||||
const config = require("./config")
|
||||
// const schedule = require("./schedule")
|
||||
@@ -15,17 +15,23 @@ module.exports = async (app) => {
|
||||
const queueName = "boobam_schedule"
|
||||
const queue = mq.Queue(queueName);
|
||||
mq.QueueScheduler(queueName);//处理延时任务
|
||||
await queue.obliterate();//清空所有定时任务
|
||||
try {
|
||||
await queue.drain();//清空所有定时任务
|
||||
await queue.obliterate();//清空所有定时任务
|
||||
} catch (err) {
|
||||
console.log("清空任务失败,新改动的任务可能没有生效:", err)
|
||||
}
|
||||
|
||||
const list = await app.load(config.path)
|
||||
//等待所有插件载入完成后,启动Worker
|
||||
app.willReadyList.push(async () => {
|
||||
for (let el of list) {
|
||||
mq.Worker(queueName, async job => {
|
||||
if (job.name===el.parse.name){
|
||||
if (job.name === el.parse.name) {
|
||||
el.res.fun(app)
|
||||
}
|
||||
});
|
||||
await queue.add(el.parse.name,{},{
|
||||
await queue.add(el.parse.name, {}, {
|
||||
repeat: el.res.time,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module.exports = {
|
||||
path:"middleware/*.js"
|
||||
path:"middleware/*/index.js"
|
||||
}
|
||||
|
||||
@@ -7,9 +7,15 @@ module.exports = async (app) => {
|
||||
let list = await app.load(config.path)
|
||||
list = app.xe.orderBy(list, "res.sort")
|
||||
list = list.filter(item => item.res.use)
|
||||
list.forEach(item => {
|
||||
app.use(async (ctx, next) => {
|
||||
return await item.res.fun(ctx, next)
|
||||
})
|
||||
list.forEach(async item => {
|
||||
if (item.res.loadFun) {
|
||||
//如果中间件定义了特殊加载方法
|
||||
await item.res.loadFun(app, item.res.fun)
|
||||
} else {
|
||||
app.use(async (ctx, next) => {
|
||||
return await item.res.fun(ctx, next, app)
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
+26
-21
@@ -12,29 +12,34 @@ module.exports = async (app) => {
|
||||
const io = require('socket.io')(server);
|
||||
let list = await app.load(config.path)
|
||||
app.io = io
|
||||
//默认命名空间
|
||||
io.on('connection', socket => {
|
||||
//通知客户端已连接
|
||||
socket.emit('open', {
|
||||
msg:"ok",
|
||||
data:{
|
||||
id:socket.id
|
||||
//等待所有插件载入完成后
|
||||
app.willReadyList.push(async () => {
|
||||
//默认命名空间
|
||||
io.on('connection', socket => {
|
||||
//通知客户端已连接
|
||||
socket.emit('open', {
|
||||
msg : "ok",
|
||||
data: {
|
||||
id: socket.id
|
||||
}
|
||||
});
|
||||
console.log('连接=>', "id:" + socket.id);
|
||||
//监听disconnect事件
|
||||
socket.on('disconnect', (eventName, callback) => {
|
||||
console.log('断开=X', "id:" + socket.id)
|
||||
})
|
||||
for (let el of list) {
|
||||
//空间名称
|
||||
// const namespace = path.basename(path.resolve(el.parse.dir, '..'))
|
||||
//事件名称
|
||||
const onname = el.parse.name
|
||||
socket.on(onname, (msg, callback) => {
|
||||
el.res(app, msg, callback)
|
||||
})
|
||||
}
|
||||
});
|
||||
console.log('连接=>',"id:"+socket.id);
|
||||
//监听disconnect事件
|
||||
socket.on('disconnect', (eventName, callback) => {
|
||||
console.log('断开=X',"id:"+socket.id)
|
||||
})
|
||||
for (let el of list) {
|
||||
//空间名称
|
||||
// const namespace = path.basename(path.resolve(el.parse.dir, '..'))
|
||||
//事件名称
|
||||
const onname = el.parse.name
|
||||
socket.on(onname, el.res)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
//覆盖启动方法
|
||||
|
||||
Reference in New Issue
Block a user