-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
86 lines (67 loc) · 1.73 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
"use strict";
const config = require("./config.json");
const common = require("./helpers/common");
const Koa = require("koa");
const hbs = require("koa-hbs");
const serve = require("koa-static");
const mount = require("koa-mount");
// for passport support
const session = require("koa-generic-session");
const bodyParser = require("koa-bodyparser");
const passport = require("koa-passport");
const app = new Koa();
exports.app = app;
exports.passport = passport;
// the auth model for passport support
require("./models/auth");
// misc handlebars helpers
require("./helpers/handlebars");
// trust proxy
app.proxy = true;
// sessions
app.keys = [config.site.secret];
app.use(session());
// body parser
app.use(bodyParser());
// authentication
app.use(passport.initialize());
app.use(passport.session());
// statically serve assets
app.use(mount("/assets", serve("./assets")));
// load up the handlebars middlewear
app.use(hbs.middleware({
viewPath: `${__dirname}/views`,
layoutsPath: `${__dirname}/views/_layouts`,
partialsPath: `${__dirname}/views/_partials`,
defaultLayout: "main"
}));
// Error handling middleware
app.use(async(ctx, next) => {
try {
await next();
if (ctx.state.api === true && ctx.status === 200) {
ctx.body = {
error: false,
result: ctx.body
};
}
} catch (err) {
ctx.app.emit("error", err, this);
ctx.status = err.status || 500;
if (ctx.state.api === true) {
return ctx.body = {
error: true,
message: err.message
};
}
await ctx.render("error", {
message: err.message
});
}
});
require("./routes");
common.log("info", `${config.site.name} is now listening on port ${config.site.port}`);
app.listen(config.site.port);
process.on("SIGINT", function exit() {
process.exit();
});