-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
80 lines (66 loc) · 1.58 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
74
75
76
77
78
79
80
'use strict'
const config = require('config')
const Koa = require('koa')
const body = require('koa-body')
const views = require('koa-views')
const nunjucks = require('nunjucks')
const path = require('path')
const session = require('koa-session')
const send = require('koa-send')
const router = require('./routes')
const app = new Koa()
app.keys = ['secret key']
app.use(body())
app.use(session(app))
const viewPath = path.join(__dirname, 'web/views')
const nunjucksEnv = new nunjucks.Environment(
new nunjucks.FileSystemLoader(viewPath)
)
app.use(views(viewPath, {
map: {
html: 'nunjucks'
},
extension: 'html',
options: {
nunjucksEnv,
noCache: true
}
}))
app.use(async (ctx, next) => {
ctx.state.config = {
apiServer: config.apiServer,
get apiToken () {
return ctx.session.token
}
}
return next()
})
app.use(async (ctx, next) => {
await next()
if (ctx.status !== 404 || !ctx.path.startsWith('/static/')) {
return
}
if (ctx.path.includes('/lib/sdk-cabinet/')) {
await send(
ctx,
ctx.path.split('/lib/sdk-cabinet/')[1],
{ root: path.resolve(__dirname, 'node_modules/shimo-sdk-cabinet/dist') }
)
return
}
const match = ctx.path.match(/\/lib\/(whatwg-fetch|es6-promise)\/(.+)/i)
if (match) {
await send(
ctx,
match[2],
{ root: path.resolve(__dirname, `node_modules/${match[1]}`) }
)
return
}
await send(
ctx, ctx.path.replace(/\/static/, ''),
{ root: path.resolve(__dirname, 'web/static') }
)
})
app.use(router.routes()).use(router.allowedMethods())
module.exports = app