2017-07-04 16:43:07 +08:00
|
|
|
import yapi from './yapi.js';
|
|
|
|
import commons from './utils/commons';
|
|
|
|
yapi.commons = commons;
|
|
|
|
import dbModule from './utils/db.js';
|
2017-07-26 22:03:18 +08:00
|
|
|
import mockServer from './middleware/mockServer.js';
|
|
|
|
import Koa from 'koa';
|
|
|
|
import koaStatic from 'koa-static';
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
|
|
|
import router from './router.js';
|
2017-08-17 11:24:46 +08:00
|
|
|
import websockify from 'koa-websocket';
|
|
|
|
import websocket from './websocket.js'
|
|
|
|
|
2017-08-23 20:41:47 +08:00
|
|
|
var compress = require('koa-compress')
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
yapi.connect = dbModule.connect();
|
2017-08-17 11:24:46 +08:00
|
|
|
const app = websockify(new Koa());
|
2017-08-11 10:25:19 +08:00
|
|
|
let indexFile = process.argv[2] === 'dev' ? 'dev.html' : 'index.html';
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
app.use(mockServer);
|
|
|
|
app.use(bodyParser());
|
|
|
|
app.use(router.routes());
|
|
|
|
app.use(router.allowedMethods());
|
2017-08-11 10:25:19 +08:00
|
|
|
|
2017-08-17 11:24:46 +08:00
|
|
|
websocket(app);
|
2017-08-11 10:25:19 +08:00
|
|
|
|
2017-08-23 20:41:47 +08:00
|
|
|
app.use(compress({
|
|
|
|
threshold: 2048,
|
|
|
|
flush: require('zlib').Z_SYNC_FLUSH
|
|
|
|
}))
|
|
|
|
|
2017-08-11 10:25:19 +08:00
|
|
|
app.use( async (ctx, next) => {
|
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
|
|
|
ctx.path = "/"
|
|
|
|
await next()
|
|
|
|
}else{
|
|
|
|
await next()
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2017-07-04 16:43:07 +08:00
|
|
|
app.use(koaStatic(
|
2017-08-11 10:25:19 +08:00
|
|
|
yapi.path.join(yapi.WEBROOT, 'static'),
|
2017-08-23 20:41:47 +08:00
|
|
|
{index: indexFile, gzip: true}
|
2017-07-26 22:03:18 +08:00
|
|
|
));
|
2017-08-11 10:25:19 +08:00
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
app.listen(yapi.WEBCONFIG.port);
|
|
|
|
commons.log(`the server is start at port ${yapi.WEBCONFIG.port}`);
|