This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (99 loc) · 3.2 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
This file is a part of libertysoil.org website
Copyright (C) 2015 Loki Education (Social Enterprise)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*eslint-env node */
import fs, { accessSync } from 'fs';
import Koa from 'koa';
import session from 'koa-generic-session';
import redisStore from 'koa-redis';
import convert from 'koa-convert';
import cors from 'kcors';
import bodyParser from 'koa-bodyparser';
import mount from 'koa-mount';
import chokidar from 'chokidar';
import Logger, { createLogger } from 'bunyan';
import createRequestLogger from './src/utils/bunyan-koa-request';
import { initApi } from './src/routing';
import initBookshelf from './src/db';
import initSphinx from './src/sphinx';
import db_config from './knexfile';
const exec_env = process.env.DB_ENV || 'development';
const streams = [];
if (exec_env !== 'test') {
streams.push({
stream: process.stderr,
level: 'info'
});
}
try {
accessSync('/var/log', fs.W_OK);
streams.push({
type: 'rotating-file',
path: '/var/log/libertysoil.log',
level: 'warn',
period: '1d', // daily rotation
count: 3 // keep 3 back copies
});
} catch (e) {
// do nothing
}
export const logger = createLogger({
name: "libertysoil",
serializers: Logger.stdSerializers,
src: true,
streams
});
const app = new Koa();
app.logger = logger;
const knexConfig = db_config[exec_env];
const bookshelf = initBookshelf(knexConfig);
const sphinx = initSphinx();
const api = initApi(bookshelf, sphinx);
app.on('error', (e) => {
logger.warn(e);
});
if (exec_env === 'development') {
logger.level('debug');
// Taken from https://github.com/glenjamin/ultimate-hot-reloading-example/blob/master/server.js
// Do "hot-reloading" of express stuff on the server
// Throw away cached modules and re-require next time
// Ensure there's no important state in there!
const watcher = chokidar.watch('./src');
watcher.on('ready', function () {
watcher.on('all', function () {
logger.debug('Clearing /src/api/ cache from server');
Object.keys(require.cache).forEach(function (id) {
if (/\/src\/api\//.test(id)) delete require.cache[id];
});
});
});
}
app.keys = ['libertysoil'];
app.use(cors({
allowHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept']
}));
app.use(bodyParser()); // for parsing application/x-www-form-urlencoded
app.use(convert(session({
store: redisStore(
{
host: '127.0.0.1',
port: 6379
}
),
key: 'connect.sid',
cookie: { signed: false }
})));
app.use(createRequestLogger({ level: 'info', logger }));
app.use(mount('/api/v1', api));
export default app;