yapi/server/router.js

329 lines
5.4 KiB
JavaScript
Raw Normal View History

import koaRouter from 'koa-router';
import interfaceController from './controllers/interface.js';
import groupController from './controllers/group.js';
import userController from './controllers/user.js';
2017-08-09 16:16:35 +08:00
import interfaceColController from './controllers/interfaceCol.js'
2017-07-03 16:16:05 +08:00
import yapi from './yapi.js';
import projectController from './controllers/project.js';
import logController from './controllers/log.js';
2017-08-10 12:13:32 +08:00
import followController from './controllers/follow.js';
2017-07-03 16:16:05 +08:00
const router = koaRouter();
2017-07-03 16:16:05 +08:00
2017-08-08 14:45:19 +08:00
const authLevel = {
admin: 0,
owner: 10,
dev: 20,
member:30,
guest:100
}
const INTERFACE_CONFIG = {
interface: {
prefix: '/interface/',
controller: interfaceController
},
user: {
prefix: '/user/',
2017-07-06 18:25:53 +08:00
controller: userController
},
group: {
prefix: '/group/',
controller: groupController
2017-07-06 19:21:54 +08:00
},
project: {
prefix: '/project/',
controller: projectController
},
2017-07-20 20:30:24 +08:00
log: {
prefix: '/log/',
controller: logController
2017-08-09 16:16:35 +08:00
},
2017-08-10 12:13:32 +08:00
follow: {
prefix: '/follow/',
controller: followController
},
2017-08-09 16:16:35 +08:00
col: {
prefix: '/col/',
controller: interfaceColController
}
2017-07-03 16:16:05 +08:00
};
2017-08-08 14:45:19 +08:00
const routerConfig = {
"group": [
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "list",
"path": "list",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "add",
"path": "add",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "up",
"path": "up",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "del",
"path": "del",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "addMember",
"path": "add_member",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "delMember",
"path": "del_member",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "getMemberList",
"path": "members",
"method": "get"
}
],
"user": [
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "login",
"path": "login",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "reg",
"path": "reg",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "list",
"path": "list",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "findById",
"path": "find",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "update",
"path": "update",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "del",
"path": "del",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "getLoginStatus",
"path": "status",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "logout",
"path": "logout",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "loginByToken",
"path": "login_by_token",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "changePassword",
"path": "change_password",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "search",
"path": "search",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-10 21:20:57 +08:00
"action": "project",
"path": "project",
2017-08-08 14:45:19 +08:00
"method": "get"
2017-08-10 15:37:20 +08:00
},{
"action": "avatar",
"path": "avatar",
"method": "get"
},{
action: "uploadAvatar",
path: "upload_avatar",
method: "post"
2017-08-08 14:45:19 +08:00
}
],
"project": [
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "add",
"path": "add",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "list",
"path": "list",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "get",
"path": "get",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "up",
"path": "up",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "del",
"path": "del",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "addMember",
"path": "add_member",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "delMember",
"path": "del_member",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "getMemberList",
"path": "get_member_list",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "search",
"path": "search",
"method": "get"
2017-08-10 10:24:24 +08:00
},
{
"action": "download",
"path": "download",
"method": "get"
2017-08-08 14:45:19 +08:00
}
],
"interface": [
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "add",
"path": "add",
"method": "post"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "list",
"path": "list",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "get",
"path": "get",
"method": "get"
},
2017-08-10 10:24:24 +08:00
{
2017-08-08 14:45:19 +08:00
"action": "up",
"path": "up",
"method": "post"
},
{
"action": "del",
"path": "del",
"method": "post"
}
],
"log": [
{
"action": "list",
"path": "list",
"method": "get"
}
2017-08-09 16:16:35 +08:00
],
2017-08-10 12:13:32 +08:00
"follow": [{
"action": "list",
"path": "list",
"method": "get"
},{
"action": "add",
"path": "add",
"method": "post"
},{
"action": "del",
"path": "del",
"method": "post"
}],
2017-08-09 16:16:35 +08:00
"col": [{
2017-08-09 20:11:39 +08:00
action: "addCol",
path: "add_col",
2017-08-09 16:16:35 +08:00
method: "post"
}, {
action: "list",
path: "list",
method: "get"
2017-08-09 20:11:39 +08:00
},{
action: "getCaseList",
path: "case_list",
method: "get"
},{
action: "addCase",
path: "add_case",
method: "post"
},{
action: "getCase",
path: "case",
method: "get"
},{
action: "upCol",
path: "up_col",
method: "post"
},{
action: "upCaseIndex",
path: "up_col_index",
method: "post"
},{
action: "delCol",
path: "del_col",
method: "post"
},{
action: "delCase",
path: "del_case",
method: "post"
}
]
2017-08-08 14:45:19 +08:00
}
2017-07-06 18:25:53 +08:00
2017-08-08 14:45:19 +08:00
for(let ctrl in routerConfig){
let actions = routerConfig[ctrl];
actions.forEach( (item) => {
createAction(ctrl, item.action, item.path, item.method);
} )
2017-08-10 10:24:24 +08:00
}
/**
2017-07-06 01:09:33 +08:00
*
* @param {*} controller controller_name
* @param {*} path request_path
* @param {*} method request_method , post get put delete ...
* @param {*} action controller_action_name
*/
2017-08-08 14:45:19 +08:00
function createAction(controller, action, path, method) {
router[method](INTERFACE_CONFIG[controller].prefix + path, async (ctx) => {
2017-07-10 11:59:13 +08:00
let inst = new INTERFACE_CONFIG[controller].controller(ctx);
2017-07-11 12:12:43 +08:00
await inst.init(ctx);
if (inst.$auth === true) {
2017-07-11 12:12:43 +08:00
await inst[action].call(inst, ctx);
} else {
2017-08-09 20:11:39 +08:00
ctx.body = yapi.commons.resReturn(null, 40011, '请登录.');
2017-07-11 12:12:43 +08:00
}
});
2017-07-06 01:09:33 +08:00
}
2017-08-10 10:24:24 +08:00
module.exports = router;