2017-07-03 16:16:05 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-11 10:25:19 +08:00
|
|
|
var _regenerator = require('babel-runtime/regenerator');
|
|
|
|
|
|
|
|
var _regenerator2 = _interopRequireDefault(_regenerator);
|
|
|
|
|
|
|
|
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
|
|
|
|
|
|
|
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _yapi = require('./yapi.js');
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _yapi2 = _interopRequireDefault(_yapi);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _commons = require('./utils/commons');
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _commons2 = _interopRequireDefault(_commons);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _db = require('./utils/db.js');
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _db2 = _interopRequireDefault(_db);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
var _mockServer = require('./middleware/mockServer.js');
|
2017-07-05 16:17:58 +08:00
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
var _mockServer2 = _interopRequireDefault(_mockServer);
|
2017-07-05 16:17:58 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _koa = require('koa');
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _koa2 = _interopRequireDefault(_koa);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _koaStatic = require('koa-static');
|
|
|
|
|
|
|
|
var _koaStatic2 = _interopRequireDefault(_koaStatic);
|
|
|
|
|
|
|
|
var _koaBodyparser = require('koa-bodyparser');
|
|
|
|
|
|
|
|
var _koaBodyparser2 = _interopRequireDefault(_koaBodyparser);
|
|
|
|
|
|
|
|
var _router = require('./router.js');
|
|
|
|
|
|
|
|
var _router2 = _interopRequireDefault(_router);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-08-17 11:24:46 +08:00
|
|
|
var _koaWebsocket = require('koa-websocket');
|
|
|
|
|
|
|
|
var _koaWebsocket2 = _interopRequireDefault(_koaWebsocket);
|
|
|
|
|
|
|
|
var _websocket = require('./websocket.js');
|
|
|
|
|
|
|
|
var _websocket2 = _interopRequireDefault(_websocket);
|
|
|
|
|
2017-07-03 16:16:05 +08:00
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
_yapi2.default.commons = _commons2.default;
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-11 16:50:17 +08:00
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
_yapi2.default.connect = _db2.default.connect();
|
2017-08-17 11:24:46 +08:00
|
|
|
var app = (0, _koaWebsocket2.default)(new _koa2.default());
|
2017-08-11 10:25:19 +08:00
|
|
|
var indexFile = process.argv[2] === 'dev' ? 'dev.html' : 'index.html';
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
app.use(_mockServer2.default);
|
2017-07-04 16:43:07 +08:00
|
|
|
app.use((0, _koaBodyparser2.default)());
|
|
|
|
app.use(_router2.default.routes());
|
|
|
|
app.use(_router2.default.allowedMethods());
|
2017-08-11 10:25:19 +08:00
|
|
|
|
2017-08-17 11:24:46 +08:00
|
|
|
(0, _websocket2.default)(app);
|
|
|
|
|
2017-08-11 10:25:19 +08:00
|
|
|
app.use(function () {
|
2017-08-22 12:20:33 +08:00
|
|
|
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx, next) {
|
2017-08-11 10:25:19 +08:00
|
|
|
return _regenerator2.default.wrap(function _callee$(_context) {
|
|
|
|
while (1) {
|
|
|
|
switch (_context.prev = _context.next) {
|
|
|
|
case 0:
|
2017-08-18 20:35:31 +08:00
|
|
|
if (!/^\/(?!api)[a-zA-Z0-9\/\-_]*$/.test(ctx.path)) {
|
2017-08-11 10:25:19 +08:00
|
|
|
_context.next = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.path = "/";
|
|
|
|
_context.next = 4;
|
|
|
|
return next();
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
_context.next = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
_context.next = 8;
|
|
|
|
return next();
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
case 'end':
|
|
|
|
return _context.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, _callee, undefined);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return function (_x, _x2) {
|
|
|
|
return _ref.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
app.use((0, _koaStatic2.default)(_yapi2.default.path.join(_yapi2.default.WEBROOT, 'static'), { index: indexFile }));
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
app.listen(_yapi2.default.WEBCONFIG.port);
|
2017-07-11 16:50:17 +08:00
|
|
|
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port);
|