优化架构,新增路由

This commit is contained in:
robin
2022-06-04 11:43:05 +08:00
parent ba9ca43729
commit 264ab8d979
22 changed files with 161 additions and 60 deletions
+13 -9
View File
@@ -1,16 +1,20 @@
'use strict';
module.exports = {
doc : "默认页面",
path : "cms/index/",
method : ["get", "post"],
middleware: [],
params : {
// path : "cms/api/index",//可以覆盖自动生成的路由地址
params: {
"age": "int?"
},
return : {},
fun : async (ctx, app) => {
const User = await app.table("User").where(where).findAll()
ctx.body = {res}
fun : async (ctx, app) => {
const body = ctx.params
const User = await app.db.table("User").find()
//成功返回的2种方式
// ctx.body = app.res.success(User,"success",200)//"success",200可以省略
return User
//异常返回的2种方式
// ctx.body = app.res.error(User, "error", 400)//"error",400可以省略
// return false
}
}
+2 -2
View File
@@ -2,11 +2,11 @@
// 定时任务
module.exports = {
time: {
cron: '0/5 * * * * ?',
cron: '0/2 * * * * ?',
// every: 2000,
// limit: 100,
},
fun: (app) => {
console.log("test",new Date());
// console.log("test",new Date());
}
}
+2 -2
View File
@@ -2,11 +2,11 @@
// 定时任务
module.exports = {
time: {
cron: '0/5 * * * * ?',
cron: '0/2 * * * * ?',
// every: 2000,
// limit: 100,
},
fun: (app) => {
console.log("test2",new Date());
// console.log("test2",new Date());
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
module.exports = (msg, callback)=>{
module.exports = (app, msg, callback) => {
console.log('接收到的消息:', msg);
}
-3
View File
@@ -1,3 +0,0 @@
module.exports = {
path:"app/*/api/*.js"
}
-13
View File
@@ -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)
})
})
}
+16
View File
@@ -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"
}
}
}
+2
View File
@@ -0,0 +1,2 @@
module.exports = {
}
+9
View File
@@ -0,0 +1,9 @@
/**
* 封装api返回结果
*/
const config = require("./config")
const body = require("./body")
module.exports = async (app) => {
app.res = body
}
+10 -4
View File
@@ -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 -1
View File
@@ -1,3 +1,3 @@
module.exports = {
path:"middleware/*.js"
path:"middleware/*/index.js"
}
+10 -4
View File
@@ -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)
})
}
})
}
+25 -20
View File
@@ -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)
}
});
})
//覆盖启动方法
+1
View File
@@ -59,6 +59,7 @@ module.exports = class Bamboo extends Koa {
// console.log("获取到的文件:", files);
files = files.map(item => {
const parse = path.parse(item);
parse.father = path.basename(path.resolve(parse.dir, '..'))
return {
parse,
res: require(path.resolve(path_root + "/" + item))
+12
View File
@@ -0,0 +1,12 @@
'use strict';
/*封装入参*/
module.exports = {
sort: 3, //排序
use : true, // 是否使用
fun : async (ctx, next, app) => {
ctx.params = ctx.request.body || ctx.query
await next()
}
}
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
path : "app/*/api/*.js",
prefix : "",//接口前缀
statusTobody: true,//是否跟随body结果(如需接口报错也返回200,那么设置为false)
}
+50
View File
@@ -0,0 +1,50 @@
'use strict';
/**
* 加载api路由
* 加载app/api目录下的文件到路由
*/
const Router = require('koa-router')
const config = require("./config")
// 错误处理
module.exports = {
sort : 999, //排序
use : true, // 是否使用
loadFun: async (app, fun) => { // 自行定义中间件的加载方式,将覆盖默认加载方法
const router = await fun(app)
app.use(router.routes()).use(router.allowedMethods())
},
fun : async (app) => {
const router = new Router({ //设置前缀
prefix: config.prefix
});
let list = await app.load(config.path)
list.forEach(item => {
const url = item.res.path ? item.res.path : '/' + item.parse.father + '/api/' + item.parse.name
router.all(url, async (ctx, next) => {
/*处理body结果*/
try {
const res = await item.res.fun(ctx, app)
if (!ctx.body) {
if (res) {
ctx.body = app.res.success(res)
} else if (res === false) {
ctx.body = app.res.error()
} else {
ctx.body = app.res.success()
}
}
} catch (err) {
console.log(err);
ctx.body = app.res.error(null, "系统错误", 500)
ctx.status = 500
}
//是否跟随body结果
ctx.status = config.statusTobody ? ctx.body.status || 400 : 200
await next();
})
})
return router
}
}
+1
View File
@@ -55,6 +55,7 @@
"koa-json-error": "^3.1.2",
"koa-logger": "^2.0.1",
"koa-redis": "^4.0.1",
"koa-router": "^7.4.0",
"log4js": "^6.4.4",
"mysql2": "^2.3.3",
"node-schedule": "^2.1.0",