优化架构,新增路由
This commit is contained in:
+11
-7
@@ -1,16 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
doc : "默认页面",
|
||||
path : "cms/index/",
|
||||
method : ["get", "post"],
|
||||
middleware: [],
|
||||
// path : "cms/api/index",//可以覆盖自动生成的路由地址
|
||||
params: {
|
||||
"age": "int?"
|
||||
},
|
||||
return : {},
|
||||
fun : async (ctx, app) => {
|
||||
const User = await app.table("User").where(where).findAll()
|
||||
ctx.body = {res}
|
||||
|
||||
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,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,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,3 +1,3 @@
|
||||
module.exports = (msg, callback)=>{
|
||||
module.exports = (app, msg, callback) => {
|
||||
console.log('接收到的消息:', msg);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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,7 +15,13 @@ module.exports = async (app) => {
|
||||
const queueName = "boobam_schedule"
|
||||
const queue = mq.Queue(queueName);
|
||||
mq.QueueScheduler(queueName);//处理延时任务
|
||||
try {
|
||||
await queue.drain();//清空所有定时任务
|
||||
await queue.obliterate();//清空所有定时任务
|
||||
} catch (err) {
|
||||
console.log("清空任务失败,新改动的任务可能没有生效:", err)
|
||||
}
|
||||
|
||||
const list = await app.load(config.path)
|
||||
//等待所有插件载入完成后,启动Worker
|
||||
app.willReadyList.push(async () => {
|
||||
|
||||
@@ -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 => {
|
||||
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)
|
||||
})
|
||||
return await item.res.fun(ctx, next, app)
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ module.exports = async (app) => {
|
||||
const io = require('socket.io')(server);
|
||||
let list = await app.load(config.path)
|
||||
app.io = io
|
||||
//等待所有插件载入完成后
|
||||
app.willReadyList.push(async () => {
|
||||
//默认命名空间
|
||||
io.on('connection', socket => {
|
||||
//通知客户端已连接
|
||||
@@ -31,10 +33,13 @@ module.exports = async (app) => {
|
||||
// const namespace = path.basename(path.resolve(el.parse.dir, '..'))
|
||||
//事件名称
|
||||
const onname = el.parse.name
|
||||
socket.on(onname, el.res)
|
||||
socket.on(onname, (msg, callback) => {
|
||||
el.res(app, msg, callback)
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
|
||||
//覆盖启动方法
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
path : "app/*/api/*.js",
|
||||
prefix : "",//接口前缀
|
||||
statusTobody: true,//是否跟随body结果(如需接口报错也返回200,那么设置为false)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user