From 2f0a7337761b77e5c6a88fd92b612cb634aa2b28 Mon Sep 17 00:00:00 2001 From: suxiaoxin Date: Tue, 15 Aug 2017 12:05:22 +0800 Subject: [PATCH] fix: mongodb auth bug and perfect mockServer --- .../Project/Interface/InterfaceList/Edit.js | 2 +- .../Interface/InterfaceList/mockEditor.js | 2 +- server/middleware/mockServer.js | 42 +++--- server/utils/db.js | 3 +- server_dist/middleware/mockServer.js | 132 +++++++++--------- server_dist/utils/db.js | 2 +- 6 files changed, 88 insertions(+), 95 deletions(-) diff --git a/client/containers/Project/Interface/InterfaceList/Edit.js b/client/containers/Project/Interface/InterfaceList/Edit.js index 55ec4acd..698458d8 100644 --- a/client/containers/Project/Interface/InterfaceList/Edit.js +++ b/client/containers/Project/Interface/InterfaceList/Edit.js @@ -29,7 +29,7 @@ class InterfaceEdit extends Component{ super(props) const {curdata, currProject} = this.props; this.state = { - mockUrl: location.protocol + '//' + location.hostname + (location.port !== "" ? ":" + location.port : "") + `/mock/${currProject._id}${currProject.basepath}/${curdata.path}` + mockUrl: location.protocol + '//' + location.hostname + (location.port !== "" ? ":" + location.port : "") + `/mock/${currProject._id}${currProject.basepath}${curdata.path}` } } diff --git a/client/containers/Project/Interface/InterfaceList/mockEditor.js b/client/containers/Project/Interface/InterfaceList/mockEditor.js index 3b3763a3..1d4d4970 100644 --- a/client/containers/Project/Interface/InterfaceList/mockEditor.js +++ b/client/containers/Project/Interface/InterfaceList/mockEditor.js @@ -86,7 +86,7 @@ function run(options) { if (typeof data === 'string') { editor.setValue(data); } else if (typeof data === 'object') { - editor.setValue(JSON.stringify(data, null, " ")) + editor.setValue(json5(data, null, " ")) } }, editor: editor diff --git a/server/middleware/mockServer.js b/server/middleware/mockServer.js index af90c30e..635535b0 100644 --- a/server/middleware/mockServer.js +++ b/server/middleware/mockServer.js @@ -8,45 +8,39 @@ module.exports = async (ctx, next) => { let hostname = ctx.hostname; let config = yapi.WEBCONFIG; + let path = ctx.path; + - if (ctx.hostname === config.webhost) { + if (path.indexOf('/mock/') !== 0) { if (next) await next(); return true; } + let paths = path.split("/"); + let projectId = paths[2]; + paths.splice(0, 3); + path = "/" + paths.join("/"); + if(!projectId){ + return ctx.body = yapi.commons.resReturn(null, 400, 'projectId不能为空'); + } + yapi.commons.log('MockServer Running...'); - let projectInst = yapi.getInst(projectModel), projects; + let projectInst = yapi.getInst(projectModel), project; try { - projects = await projectInst.getByDomain(hostname); + project = await projectInst.get(projectId); } catch (e) { return ctx.body = yapi.commons.resReturn(null, 403, e.message); } - let matchProject = [], maxBasepath = 0; - - for (let i = 0, l = projects.length; i < l; i++) { - let project = projects[i]; - if(ctx.path && project.basepath == ""){ - matchProject = project; - } - else if (ctx.path && ctx.path.indexOf(project.basepath) === 0) { - if (project.basepath.length > maxBasepath) { - maxBasepath = project.basepath.length; - matchProject = project; - } - } + if (project === false) { + return ctx.body = yapi.commons.resReturn(null, 400, '不存在的项目'); } - - if (matchProject === false) { - return ctx.body = yapi.commons.resReturn(null, 400, '不存在的domain'); - } - - let project = matchProject, interfaceData; + + let interfaceData; let interfaceInst = yapi.getInst(interfaceModel); try { - - interfaceData = await interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method); + interfaceData = await interfaceInst.getByPath(project._id, path.substr(project.basepath.length), ctx.method); if (!interfaceData || interfaceData.length === 0) { //非正常跨域预检请求回应 if(ctx.method === 'OPTIONS'){ diff --git a/server/utils/db.js b/server/utils/db.js index a427a0cf..f60a3cbb 100644 --- a/server/utils/db.js +++ b/server/utils/db.js @@ -18,10 +18,11 @@ function connect() { let config = yapi.WEBCONFIG; let options = {}; - if (config.user) { + if (config.db.user) { options.user = config.db.user; options.pass = config.db.pass; } + let db = mongoose.connect(`mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`, options); diff --git a/server_dist/middleware/mockServer.js b/server_dist/middleware/mockServer.js index 2ee0428d..19ce4f24 100644 --- a/server_dist/middleware/mockServer.js +++ b/server_dist/middleware/mockServer.js @@ -12,9 +12,9 @@ var _yapi = require('../yapi.js'); var _yapi2 = _interopRequireDefault(_yapi); -var _project2 = require('../models/project.js'); +var _project = require('../models/project.js'); -var _project3 = _interopRequireDefault(_project2); +var _project2 = _interopRequireDefault(_project); var _interface = require('../models/interface.js'); @@ -28,8 +28,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de module.exports = function () { var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { - var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst, res; - + var hostname, config, path, paths, projectId, projectInst, project, interfaceData, interfaceInst, res; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { @@ -38,82 +37,81 @@ module.exports = function () { hostname = ctx.hostname; config = _yapi2.default.WEBCONFIG; + path = ctx.path; - if (!(ctx.hostname === config.webhost)) { - _context.next = 8; + if (!(path.indexOf('/mock/') !== 0)) { + _context.next = 9; break; } if (!next) { - _context.next = 7; + _context.next = 8; break; } - _context.next = 7; + _context.next = 8; return next(); - case 7: + case 8: return _context.abrupt('return', true); - case 8: + case 9: + paths = path.split("/"); + projectId = paths[2]; - _yapi2.default.commons.log('MockServer Running...'); - projectInst = _yapi2.default.getInst(_project3.default), projects = void 0; - _context.prev = 10; - _context.next = 13; - return projectInst.getByDomain(hostname); + paths.splice(0, 3); + path = "/" + paths.join("/"); - case 13: - projects = _context.sent; - _context.next = 19; - break; - - case 16: - _context.prev = 16; - _context.t0 = _context['catch'](10); - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message)); - - case 19: - matchProject = [], maxBasepath = 0; - - - for (i = 0, l = projects.length; i < l; i++) { - _project = projects[i]; - - if (ctx.path && _project.basepath == "") { - matchProject = _project; - } else if (ctx.path && ctx.path.indexOf(_project.basepath) === 0) { - if (_project.basepath.length > maxBasepath) { - maxBasepath = _project.basepath.length; - matchProject = _project; - } - } - } - - if (!(matchProject === false)) { - _context.next = 23; + if (projectId) { + _context.next = 15; break; } - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的domain')); + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'projectId不能为空')); + + case 15: + + _yapi2.default.commons.log('MockServer Running...'); + projectInst = _yapi2.default.getInst(_project2.default), project = void 0; + _context.prev = 17; + _context.next = 20; + return projectInst.get(projectId); + + case 20: + project = _context.sent; + _context.next = 26; + break; case 23: - project = matchProject, interfaceData = void 0; - interfaceInst = _yapi2.default.getInst(_interface2.default); - _context.prev = 25; - _context.next = 28; - return interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method); + _context.prev = 23; + _context.t0 = _context['catch'](17); + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message)); + + case 26: + if (!(project === false)) { + _context.next = 28; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的项目')); case 28: + interfaceData = void 0; + interfaceInst = _yapi2.default.getInst(_interface2.default); + _context.prev = 30; + _context.next = 33; + return interfaceInst.getByPath(project._id, path.substr(project.basepath.length), ctx.method); + + case 33: interfaceData = _context.sent; if (!(!interfaceData || interfaceData.length === 0)) { - _context.next = 35; + _context.next = 40; break; } if (!(ctx.method === 'OPTIONS')) { - _context.next = 34; + _context.next = 39; break; } @@ -121,54 +119,54 @@ module.exports = function () { ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); return _context.abrupt('return', ctx.body = 'ok'); - case 34: + case 39: return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api')); - case 35: + case 40: if (!(interfaceData.length > 1)) { - _context.next = 37; + _context.next = 42; break; } return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api,请检查数据库')); - case 37: + case 42: interfaceData = interfaceData[0]; ctx.set("Access-Control-Allow-Origin", "*"); if (!(interfaceData.res_body_type === 'json')) { - _context.next = 48; + _context.next = 53; break; } - _context.prev = 40; + _context.prev = 45; res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)); return _context.abrupt('return', ctx.body = res); - case 45: - _context.prev = 45; - _context.t1 = _context['catch'](40); + case 50: + _context.prev = 50; + _context.t1 = _context['catch'](45); return _context.abrupt('return', ctx.body = { errcode: 400, errmsg: 'mock json数据格式有误', data: interfaceData.res_body }); - case 48: + case 53: return _context.abrupt('return', ctx.body = interfaceData.res_body); - case 51: - _context.prev = 51; - _context.t2 = _context['catch'](25); + case 56: + _context.prev = 56; + _context.t2 = _context['catch'](30); return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t2.message)); - case 54: + case 59: case 'end': return _context.stop(); } } - }, _callee, undefined, [[10, 16], [25, 51], [40, 45]]); + }, _callee, undefined, [[17, 23], [30, 56], [45, 50]]); })); return function (_x, _x2) { diff --git a/server_dist/utils/db.js b/server_dist/utils/db.js index 21aae9bd..0f95f066 100644 --- a/server_dist/utils/db.js +++ b/server_dist/utils/db.js @@ -30,7 +30,7 @@ function connect() { var config = _yapi2.default.WEBCONFIG; var options = {}; - if (config.user) { + if (config.db.user) { options.user = config.db.user; options.pass = config.db.pass; }