-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·63 lines (53 loc) · 1.83 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 storageCreator = require('./utils/storage')
const Koa = require('koa');
const koaStatic = require('koa-static');
// const bodyParser = require('koa-bodyparser');
const koaBody = require('koa-body');
const router = require('./router.js');
global.storageCreator = storageCreator;
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(koaBody({ multipart: true, jsonLimit: '2mb', formLimit: '1mb', textLimit: '1mb' }));
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(
`服务已启动,请打开下面链接访问: \nhttp://127.0.0.1${
yapi.WEBCONFIG.port == '80' ? '' : ':' + yapi.WEBCONFIG.port
}/`
);