mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-03 04:50:32 +08:00
60 lines
1.6 KiB
JavaScript
Executable File
60 lines
1.6 KiB
JavaScript
Executable File
process.env.NODE_PATH = __dirname;
|
|
require('module').Module._initPaths();
|
|
|
|
const yapi = require('./yapi.js');
|
|
const commons = require('./utils/commons');
|
|
yapi.commons = commons;
|
|
const dbModule = require('./utils/db.js');
|
|
yapi.connect = dbModule.connect();
|
|
const mockServer = require('./middleware/mockServer.js');
|
|
const plugins = require('./plugin.js');
|
|
const websockify = require('koa-websocket');
|
|
const websocket = require('./websocket.js');
|
|
|
|
const Koa = require('koa');
|
|
const koaStatic = require('koa-static');
|
|
const bodyParser = require('koa-bodyparser');
|
|
const router = require('./router.js');
|
|
|
|
let indexFile = process.argv[2] === 'dev' ? 'dev.html' : 'index.html';
|
|
|
|
|
|
const app = websockify(new Koa());
|
|
app.proxy = true;
|
|
yapi.app = app;
|
|
|
|
app.use(bodyParser({multipart: true}));
|
|
app.use(mockServer);
|
|
app.use(router.routes());
|
|
app.use(router.allowedMethods());
|
|
|
|
websocket(app);
|
|
|
|
app.use( async (ctx, next) => {
|
|
if( /^\/(?!api)[a-zA-Z0-9\/\-_]*$/.test(ctx.path) ){
|
|
ctx.path = "/"
|
|
await next()
|
|
}else{
|
|
await next()
|
|
}
|
|
|
|
})
|
|
|
|
app.use( async (ctx, next)=>{
|
|
if(ctx.path.indexOf('/prd') === 0){
|
|
ctx.set('Cache-Control', 'max-age=8640000000');
|
|
if(yapi.commons.fileExist( yapi.path.join(yapi.WEBROOT, 'static', ctx.path+'.gz') )){
|
|
ctx.set('Content-Encoding', 'gzip')
|
|
ctx.path = ctx.path + '.gz';
|
|
}
|
|
}
|
|
await next()
|
|
})
|
|
|
|
app.use(koaStatic(
|
|
yapi.path.join(yapi.WEBROOT, 'static'),
|
|
{index: indexFile, gzip: true}
|
|
));
|
|
|
|
app.listen(yapi.WEBCONFIG.port);
|
|
commons.log(`the server is start at 127.0.0.1${ yapi.WEBCONFIG.port == '80' ? '' : ':' + yapi.WEBCONFIG.port}`);
|