优化架构,新增路由
This commit is contained in:
+13
-9
@@ -1,16 +1,20 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
doc : "默认页面",
|
// path : "cms/api/index",//可以覆盖自动生成的路由地址
|
||||||
path : "cms/index/",
|
params: {
|
||||||
method : ["get", "post"],
|
|
||||||
middleware: [],
|
|
||||||
params : {
|
|
||||||
"age": "int?"
|
"age": "int?"
|
||||||
},
|
},
|
||||||
return : {},
|
fun : async (ctx, app) => {
|
||||||
fun : async (ctx, app) => {
|
|
||||||
const User = await app.table("User").where(where).findAll()
|
const body = ctx.params
|
||||||
ctx.body = {res}
|
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 = {
|
module.exports = {
|
||||||
time: {
|
time: {
|
||||||
cron: '0/5 * * * * ?',
|
cron: '0/2 * * * * ?',
|
||||||
// every: 2000,
|
// every: 2000,
|
||||||
// limit: 100,
|
// limit: 100,
|
||||||
},
|
},
|
||||||
fun: (app) => {
|
fun: (app) => {
|
||||||
console.log("test",new Date());
|
// console.log("test",new Date());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
// 定时任务
|
// 定时任务
|
||||||
module.exports = {
|
module.exports = {
|
||||||
time: {
|
time: {
|
||||||
cron: '0/5 * * * * ?',
|
cron: '0/2 * * * * ?',
|
||||||
// every: 2000,
|
// every: 2000,
|
||||||
// limit: 100,
|
// limit: 100,
|
||||||
},
|
},
|
||||||
fun: (app) => {
|
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);
|
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
|
||||||
|
}
|
||||||
+10
-4
@@ -3,7 +3,7 @@
|
|||||||
* 加载schedule文件夹下的任务
|
* 加载schedule文件夹下的任务
|
||||||
* https://blog.pincman.com/archives/18/
|
* https://blog.pincman.com/archives/18/
|
||||||
* https://docs.bullmq.io
|
* https://docs.bullmq.io
|
||||||
*
|
* https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md
|
||||||
*/
|
*/
|
||||||
const config = require("./config")
|
const config = require("./config")
|
||||||
// const schedule = require("./schedule")
|
// const schedule = require("./schedule")
|
||||||
@@ -15,17 +15,23 @@ module.exports = async (app) => {
|
|||||||
const queueName = "boobam_schedule"
|
const queueName = "boobam_schedule"
|
||||||
const queue = mq.Queue(queueName);
|
const queue = mq.Queue(queueName);
|
||||||
mq.QueueScheduler(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)
|
const list = await app.load(config.path)
|
||||||
//等待所有插件载入完成后,启动Worker
|
//等待所有插件载入完成后,启动Worker
|
||||||
app.willReadyList.push(async () => {
|
app.willReadyList.push(async () => {
|
||||||
for (let el of list) {
|
for (let el of list) {
|
||||||
mq.Worker(queueName, async job => {
|
mq.Worker(queueName, async job => {
|
||||||
if (job.name===el.parse.name){
|
if (job.name === el.parse.name) {
|
||||||
el.res.fun(app)
|
el.res.fun(app)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await queue.add(el.parse.name,{},{
|
await queue.add(el.parse.name, {}, {
|
||||||
repeat: el.res.time,
|
repeat: el.res.time,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
path:"middleware/*.js"
|
path:"middleware/*/index.js"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,15 @@ module.exports = async (app) => {
|
|||||||
let list = await app.load(config.path)
|
let list = await app.load(config.path)
|
||||||
list = app.xe.orderBy(list, "res.sort")
|
list = app.xe.orderBy(list, "res.sort")
|
||||||
list = list.filter(item => item.res.use)
|
list = list.filter(item => item.res.use)
|
||||||
list.forEach(item => {
|
list.forEach(async item => {
|
||||||
app.use(async (ctx, next) => {
|
if (item.res.loadFun) {
|
||||||
return await item.res.fun(ctx, next)
|
//如果中间件定义了特殊加载方法
|
||||||
})
|
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);
|
const io = require('socket.io')(server);
|
||||||
let list = await app.load(config.path)
|
let list = await app.load(config.path)
|
||||||
app.io = io
|
app.io = io
|
||||||
//默认命名空间
|
//等待所有插件载入完成后
|
||||||
io.on('connection', socket => {
|
app.willReadyList.push(async () => {
|
||||||
//通知客户端已连接
|
//默认命名空间
|
||||||
socket.emit('open', {
|
io.on('connection', socket => {
|
||||||
msg:"ok",
|
//通知客户端已连接
|
||||||
data:{
|
socket.emit('open', {
|
||||||
id:socket.id
|
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)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//覆盖启动方法
|
//覆盖启动方法
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ module.exports = class Bamboo extends Koa {
|
|||||||
// console.log("获取到的文件:", files);
|
// console.log("获取到的文件:", files);
|
||||||
files = files.map(item => {
|
files = files.map(item => {
|
||||||
const parse = path.parse(item);
|
const parse = path.parse(item);
|
||||||
|
parse.father = path.basename(path.resolve(parse.dir, '..'))
|
||||||
return {
|
return {
|
||||||
parse,
|
parse,
|
||||||
res: require(path.resolve(path_root + "/" + item))
|
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-json-error": "^3.1.2",
|
||||||
"koa-logger": "^2.0.1",
|
"koa-logger": "^2.0.1",
|
||||||
"koa-redis": "^4.0.1",
|
"koa-redis": "^4.0.1",
|
||||||
|
"koa-router": "^7.4.0",
|
||||||
"log4js": "^6.4.4",
|
"log4js": "^6.4.4",
|
||||||
"mysql2": "^2.3.3",
|
"mysql2": "^2.3.3",
|
||||||
"node-schedule": "^2.1.0",
|
"node-schedule": "^2.1.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user