-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (86 loc) · 2.3 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// @flow
import _ from 'lodash';
import Koa from 'koa';
import path from 'path';
import Pug from 'koa-pug';
import Router from 'koa-router';
import koaLogger from 'koa-logger';
import serve from 'koa-static';
import middleware from 'koa-webpack';
import bodyParser from 'koa-bodyparser';
import session from 'koa-generic-session';
import flash from 'koa-flash-simple';
import Rollbar from 'rollbar';
import methodOverride from 'koa-methodoverride';
import webpackConfig from './webpack.config.babel';
import addRoutes from './routes';
import container from './container';
let rollbar = {};
if (process.env.NODE_ENV === 'production') {
rollbar = new Rollbar(process.env.ROLLBAR_TOKEN);
}
export default () => {
const app = new Koa();
app.keys = ['some secret hurr'];
app.use(session(app));
app.use(flash());
app.use(async (ctx, next) => {
ctx.state = {
flash: ctx.flash,
userId: ctx.session.userId,
userAvatar: ctx.session.userAvatar,
isSignedIn: () => ctx.session.userId !== undefined,
};
await next();
});
app.use(async (ctx, next) => {
try {
await next();
if (ctx.response.status === 404 && !ctx.response.body) {
ctx.throw(404);
}
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
ctx.render('error/index', {
status: ctx.status,
message: ctx.message,
});
}
});
app.on('error', (err) => {
if (process.env.NODE_ENV === 'production') {
rollbar.log(err);
}
console.log(err);
});
app.use(bodyParser());
app.use(methodOverride('_method'));
app.use(serve(path.join(__dirname, 'public')));
if (process.env.NODE_ENV !== 'production') {
app.use(middleware({
config: webpackConfig,
}));
}
app.use(koaLogger());
const router = new Router();
addRoutes(router, container);
app.use(router.allowedMethods());
app.use(router.routes());
const pug = new Pug({
viewPath: path.join(__dirname, 'views'),
noCache: process.env.NODE_ENV === 'development',
debug: true,
pretty: true,
compileDebug: true,
locals: [],
basedir: path.join(__dirname, 'views'),
helperPath: [
{ _ },
{ urlFor: (...args) => router.url(...args) },
],
});
pug.use(app);
return app;
};