yapi/server_dist/router.js

142 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +08:00
'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
2017-07-03 16:16:05 +08:00
var _koaRouter = require('koa-router');
var _koaRouter2 = _interopRequireDefault(_koaRouter);
var _interface = require('./controllers/interface.js');
2017-07-03 16:16:05 +08:00
var _interface2 = _interopRequireDefault(_interface);
var _group = require('./controllers/group.js');
var _group2 = _interopRequireDefault(_group);
2017-07-06 20:55:02 +08:00
var _user = require('./controllers/user.js');
var _user2 = _interopRequireDefault(_user);
2017-07-06 19:21:54 +08:00
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project = require('./controllers/project.js');
var _project2 = _interopRequireDefault(_project);
2017-07-05 17:59:53 +08:00
2017-07-03 16:16:05 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var router = (0, _koaRouter2.default)();
var INTERFACE_CONFIG = {
interface: {
prefix: '/interface/',
controller: _interface2.default
},
user: {
prefix: '/user/',
2017-07-06 20:55:02 +08:00
controller: _user2.default
},
group: {
prefix: '/group/',
controller: _group2.default
2017-07-06 19:21:54 +08:00
},
project: {
prefix: '/project/',
controller: _project2.default
}
2017-07-03 16:16:05 +08:00
};
2017-07-06 20:55:02 +08:00
//group
createAction('group', 'list', 'get', 'list');
createAction('group', 'add', 'post', 'add');
createAction('group', 'up', 'post', 'up');
createAction('group', 'del', 'post', 'del');
2017-07-06 20:55:02 +08:00
//user
createAction('user', 'login', 'post', 'login');
createAction('user', 'reg', 'post', 'reg');
createAction('user', 'list', 'get', 'list');
2017-07-12 17:01:27 +08:00
createAction('user', 'find', 'post', 'findById');
2017-07-06 20:55:02 +08:00
createAction('user', 'update', 'post', 'update');
createAction('user', 'del', 'post', 'del');
2017-07-11 12:12:43 +08:00
createAction('user', 'status', 'get', 'getLoginStatus');
2017-07-11 16:50:17 +08:00
createAction('user', 'logout', 'get', 'logout');
2017-07-12 17:01:27 +08:00
createAction('user', 'login_by_token', 'post', 'loginByToken');
2017-07-06 20:55:02 +08:00
2017-07-06 19:21:54 +08:00
//project
createAction('project', 'add', 'post', 'add');
createAction('project', 'list', 'get', 'list');
createAction('project', 'get', 'get', 'get');
createAction('project', 'up', 'post', 'up');
createAction('project', 'del', 'post', 'del');
2017-07-10 11:56:53 +08:00
createAction('project', 'add_member', 'post', 'addMember');
createAction('project', 'del_member', 'post', 'delMember');
//interface
createAction('interface', 'add', 'post', 'add');
createAction('interface', 'list', 'get', 'list');
createAction('interface', 'get', 'get', 'get');
createAction('interface', 'up', 'post', 'up');
createAction('interface', 'del', 'post', 'del');
2017-07-06 19:21:54 +08:00
/**
2017-07-11 16:53:14 +08:00
*
* @param {*} controller controller_name
* @param {*} path request_path
* @param {*} method request_method , post get put delete ...
* @param {*} action controller_action_name
*/
function createAction(controller, path, method, action) {
var _this = this;
router[method](INTERFACE_CONFIG[controller].prefix + path, function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var inst;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
2017-07-10 11:59:13 +08:00
inst = new INTERFACE_CONFIG[controller].controller(ctx);
_context.next = 3;
2017-07-11 12:12:43 +08:00
return inst.init(ctx);
case 3:
2017-07-11 12:12:43 +08:00
if (!(inst.$auth === true)) {
2017-07-11 16:50:17 +08:00
_context.next = 8;
2017-07-11 12:12:43 +08:00
break;
}
2017-07-11 16:50:17 +08:00
_context.next = 6;
2017-07-11 12:12:43 +08:00
return inst[action].call(inst, ctx);
2017-07-11 16:50:17 +08:00
case 6:
_context.next = 9;
2017-07-11 12:12:43 +08:00
break;
2017-07-11 16:50:17 +08:00
case 8:
2017-07-11 12:12:43 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 400, 'Without Permission.');
2017-07-11 16:50:17 +08:00
case 9:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}());
}
2017-07-03 16:16:05 +08:00
module.exports = router;