This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
76 lines (62 loc) · 1.72 KB
/
server.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
'use strict'
// builtin
const Debug = require('debug')
const path = require('path')
const crypto = require('crypto')
// middleware
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const serve = require('./lib/koa-file-server')
const logger = require('./lib/koa-morgan')
const json = require('koa-json')
const bodyparser = require('koa-bodyparser')
const error = require('koa-error')
const session = require('koa-session')
const CSRF = require('koa-csrf').default
const sqlite = require('./lib/sqlite')
// instantiations
const debug = new Debug('app:server.js')
const app = new Koa()
const router = new Router()
const dev = app.env === 'development'
const prod = app.env === 'production'
const test = app.env === 'test'
app.keys = [crypto.prng(50).toString('hex'), crypto.prng(50).toString('hex')]
debug('error page middleware')
app.use(error({
engine: 'pug',
template: `${path.resolve('./views')}/error.pug`
}))
debug('setup logging')
let format = dev ? 'tiny' : 'short'
if (!test) app.use(logger(format))
debug('init middleware')
app.use(bodyparser())
app.use(json())
app.use(sqlite())
app.use(session(app))
// app.use(new CSRF())
app.use(serve({
root: './public',
maxage: 60 * 60 * 1000,
etag: {
algorithm: 'md5'
}
}))
app.use(views(path.resolve('./views'), {
extension: 'pug'
}))
const viewHelpers = require('./views/helpers')
app.use(async (ctx, next) => {
ctx.state.csrf = ctx.csrf
Object.assign(ctx.state, viewHelpers)
await next()
})
debug('init routes')
require('./routes/index')(router)
require('./routes/users')(router)
require('./routes/articles')(router)
require('./routes/comments')(router)
app.use(router.routes(), router.allowedMethods())
module.exports = app