-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
87 lines (69 loc) · 2.13 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
// app dependency
const http = require('http');
const https = require('https');
const fs = require('fs');
const koa = require('koa');
const koaViews = require('koa-views');
const koaLogger = require('koa-logger');
const koaStatic = require('koa-static');
const koaRouter = require('koa-trie-router');
const koaSession = require('koa-session');
const koaMount = require('koa-mount');
const koaBody = require('koa-body');
const koaCSRF = require('koa-csrf');
const koaSSL = require('koa-sslify');
const koaFlash = require('koa-better-flash');
const grant = require('grant-koa');
const setupRouter = require(__dirname + '/routes');
const defaultSettings = require(__dirname + '/animeshot-example.json');
const customSettings = require(__dirname + '/animeshot.json');
const settings = Object.assign({}, defaultSettings, customSettings);
const i18n = require(__dirname + '/i18n.json');
settings.site.i18n = i18n;
// define routing
const router = new koaRouter();
setupRouter(router);
// define app
const app = new koa();
app.use(koaLogger());
if (!settings.site.server.has_proxy) {
app.use(koaSSL());
}
app.use(koaStatic(__dirname + '/public', {
maxage: 1000 * 86400 * 30
}));
app.use(koaViews(__dirname + '/views', {
extension: 'pug',
options: {
cache: true
}
}));
app.use(koaBody({
multipart: true,
urlencoded: false,
text: false,
json: false,
strict: true,
formidable: {
maxFileSize: 20 * 1024 * 1024
}
}));
app.use(new koaCSRF());
app.keys = settings.cookie.keys;
app.use(koaSession(settings.cookie.session, app));
app.use(koaFlash());
app.use(koaMount(grant(settings.oauth)));
app.use(router.middleware());
// whether we use a reverse proxy like nginx
if (settings.site.server.has_proxy) {
// start sever, let proxy handle the rest
app.listen(settings.site.server.server_port);
} else {
const ssl = {
key: fs.readFileSync(__dirname + settings.site.server.ssl_key),
cert: fs.readFileSync(__dirname + settings.site.server.ssl_certificate)
};
// start server
http.createServer(app.callback()).listen(settings.site.server.http_port);
https.createServer(ssl, app.callback()).listen(settings.site.server.https_port);
}