forked from easy-mock/easy-mock
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
73 lines (65 loc) · 2.01 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
64
65
66
67
68
69
70
71
72
73
'use strict'
const Koa = require('koa')
const path = require('path')
const config = require('config')
const koaJwt = require('koa-jwt')
const cors = require('@koa/cors')
const koaBody = require('koa-body')
const onerror = require('koa-onerror')
const favicon = require('koa-favicon')
const validate = require('koa-validate')
const pathToRegexp = require('path-to-regexp')
const staticCache = require('koa-static-cache')
const util = require('./util')
const logger = require('./util/logger')
const middleware = require('./middlewares')
const routerConfig = require('./router-config')
const app = module.exports = new Koa()
const uploadConf = config.get('upload')
const jwtSecret = config.get('jwt.secret')
util.init()
onerror(app)
validate(app)
app
.use(middleware.ipFilter)
.use(favicon(path.join(__dirname, '/public/images/icon.png')))
.use(serve('/dist', './dist'))
.use(serve('/public', './public'))
.use(serve('/upload', path.resolve(__dirname, 'config', uploadConf.dir)))
.use(logger)
.use(middleware.util)
.use(cors({ credentials: true, maxAge: 2592000 }))
.use(koaJwt({ secret: jwtSecret }).unless((ctx) => {
if (/^\/api/.test(ctx.path)) {
return pathToRegexp([
'/api/u/login',
'/api/u/register',
'/api/mock/by_projects',
'/api/mock/export',
'/api/wallpaper'
]).test(ctx.path)
}
return true
}))
.use(koaBody({ multipart: true }))
.use(routerConfig.mock.routes())
.use(routerConfig.mock.allowedMethods())
.use(routerConfig.api.routes())
.use(routerConfig.api.allowedMethods())
app.proxy = config.get('proxy')
/* istanbul ignore if */
if (!module.parent) {
const port = config.get('port')
const host = config.get('host')
app.use(require('./middlewares/view').render(app))
app.listen(port, host)
console.log(`server started at http://${host}:${port}`)
}
function serve (prefix, filePath) {
return staticCache(path.resolve(__dirname, filePath), {
prefix: prefix,
gzip: true,
dynamic: true,
maxAge: 60 * 60 * 24 * 30
})
}