2017-07-10 20:02:44 +08:00
|
|
|
|
import yapi from '../yapi.js';
|
2017-07-27 19:49:26 +08:00
|
|
|
|
import projectModel from '../models/project.js';
|
|
|
|
|
import interfaceModel from '../models/interface.js';
|
|
|
|
|
import Mock from 'mockjs';
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
module.exports = async (ctx, next) => {
|
2017-07-27 19:49:26 +08:00
|
|
|
|
yapi.commons.log('Server Recevie Request...');
|
|
|
|
|
|
2017-07-20 15:26:26 +08:00
|
|
|
|
let hostname = ctx.hostname;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
let config = yapi.WEBCONFIG;
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
|
|
|
|
if (ctx.hostname === config.webhost) {
|
|
|
|
|
if (next) await next();
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
|
|
|
|
yapi.commons.log('MockServer Running...');
|
2017-07-10 20:02:44 +08:00
|
|
|
|
let projectInst = yapi.getInst(projectModel), projects;
|
2017-07-27 19:49:26 +08:00
|
|
|
|
try {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
projects = await projectInst.getByDomain(hostname);
|
2017-07-27 19:49:26 +08:00
|
|
|
|
} catch (e) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 403, e.message);
|
|
|
|
|
}
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
2017-07-26 15:31:16 +08:00
|
|
|
|
let matchProject = [], maxBasepath = 0;
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
|
|
|
|
for (let i = 0, l = projects.length; i < l; i++) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
let project = projects[i];
|
2017-08-01 15:47:06 +08:00
|
|
|
|
if(ctx.path && project.basepath == ""){
|
|
|
|
|
matchProject = project;
|
|
|
|
|
}
|
|
|
|
|
else if (ctx.path && ctx.path.indexOf(project.basepath) === 0) {
|
2017-07-27 19:49:26 +08:00
|
|
|
|
if (project.basepath.length > maxBasepath) {
|
2017-07-17 15:51:15 +08:00
|
|
|
|
maxBasepath = project.basepath.length;
|
|
|
|
|
matchProject = project;
|
|
|
|
|
}
|
2017-07-10 20:02:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 19:49:26 +08:00
|
|
|
|
if (matchProject === false) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '不存在的domain');
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 15:51:15 +08:00
|
|
|
|
let project = matchProject, interfaceData;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
let interfaceInst = yapi.getInst(interfaceModel);
|
|
|
|
|
|
2017-07-27 19:49:26 +08:00
|
|
|
|
try {
|
|
|
|
|
|
2017-08-10 12:14:35 +08:00
|
|
|
|
interfaceData = await interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method);
|
2017-07-27 19:49:26 +08:00
|
|
|
|
if (!interfaceData || interfaceData.length === 0) {
|
2017-07-31 10:15:16 +08:00
|
|
|
|
//非正常跨域预检请求回应
|
|
|
|
|
if(ctx.method === 'OPTIONS'){
|
|
|
|
|
ctx.set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
|
|
|
|
|
return ctx.body = 'ok'
|
|
|
|
|
}
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 404, '不存在的api');
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 19:49:26 +08:00
|
|
|
|
if (interfaceData.length > 1) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 405, '存在多个api,请检查数据库');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interfaceData = interfaceData[0];
|
2017-07-31 10:15:16 +08:00
|
|
|
|
ctx.set("Access-Control-Allow-Origin", "*")
|
2017-07-27 19:49:26 +08:00
|
|
|
|
if (interfaceData.res_body_type === 'json') {
|
2017-08-01 15:47:06 +08:00
|
|
|
|
try{
|
2017-08-10 12:14:35 +08:00
|
|
|
|
const res = Mock.mock(
|
2017-08-01 15:47:06 +08:00
|
|
|
|
yapi.commons.json_parse(interfaceData.res_body)
|
|
|
|
|
);
|
2017-08-10 12:14:35 +08:00
|
|
|
|
return ctx.body = res;
|
2017-08-01 15:47:06 +08:00
|
|
|
|
}catch(e){
|
|
|
|
|
return ctx.body = {
|
|
|
|
|
errcode: 400,
|
|
|
|
|
errmsg: 'mock json数据格式有误',
|
|
|
|
|
data: interfaceData.res_body
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-10 20:02:44 +08:00
|
|
|
|
}
|
|
|
|
|
return ctx.body = interfaceData.res_body;
|
2017-07-27 19:49:26 +08:00
|
|
|
|
} catch (e) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 409, e.message);
|
|
|
|
|
}
|
2017-08-10 12:14:35 +08:00
|
|
|
|
};
|