This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
/
index.js
83 lines (75 loc) · 2.58 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
/*
#teamchatviz
Copyright (C) 2016 Moovel Group GmbH, Haupstaetter str. 149, 70188, Stuttgart, Germany hallo@moovel.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
import Koa from 'koa';
import KoaRouter from 'koa-router';
import bodyParser from 'koa-bodyparser';
import cors from 'kcors';
import convert from 'koa-convert';
import session from 'koa-generic-session';
import passport from 'koa-passport';
import koaStatic from 'koa-static';
import mount from 'koa-mount';
import pgStore from './pg-store';
import './auth';
import api from './api';
import Promise from 'bluebird';
import fs from 'fs';
import auth from 'koa-basic-auth';
import ms from 'ms';
import config from './config';
import errorHandler from './error';
import demoAuth from './demo-auth';
import conditional from 'koa-conditional-get';
import etag from 'koa-etag';
import compress from 'koa-compress';
if (!config.sessionSecret) {
throw new Error('Define config.sessionSecret');
}
const apiApp = new Koa();
apiApp.keys = [ config.sessionSecret ];
apiApp.use(errorHandler);
apiApp.use(compress());
apiApp.use(bodyParser());
apiApp.use(convert(session({
store: pgStore,
rolling: true,
cookie: {
maxage: ms('7 days'),
},
})));
apiApp.use(passport.initialize());
apiApp.use(passport.session());
apiApp.use(demoAuth);
apiApp.use(api.routes());
apiApp.use(api.allowedMethods());
const app = new Koa();
app.keys = [ config.sessionSecret ];
app.use(errorHandler);
if (config.basicAuthUser) {
const basicAuth = auth({ name: config.basicAuthUser, pass: config.basicAuthPassword });
app.use(basicAuth);
}
app.use(mount('/api', apiApp));
app.use(conditional());
app.use(etag());
app.use(compress());
app.use(mount('/', koaStatic(__dirname + '/../client')));
app.use(async (ctx) => {
// return index.html for anything that is not handled
ctx.body = await Promise.fromCallback(cb => fs.readFile(__dirname + '/../client/index.html', 'utf-8', cb));
});
export default app;