-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserver.js
47 lines (35 loc) · 1.11 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
'use strict';
const http = require('http');
const logger = require('logfmt');
const jackrabbit = require('jackrabbit');
http.globalAgent.maxSockets = Infinity;
const web = require('./web');
const RABBIT_URL =
process.env.CLOUDAMQP_URL || 'amqp://guest:guest@localhost:5672';
const PORT = process.env.PORT || 5000;
const __DEV__ = process.env.NODE_ENV === 'development';
function start() {
logger.log({ type: 'info', message: 'starting server' });
let server;
const broker = jackrabbit(RABBIT_URL);
broker.once('connected', listen);
broker.once('disconnected', exit.bind(this, 'disconnected'));
process.on('SIGTERM', exit);
function listen() {
const app = web(__DEV__);
server = http.createServer(app);
server.listen(PORT, err => {
if (err) throw err;
logger.log({
type: 'info',
message: `running on port ${PORT} in ${process.env.NODE_ENV} mode`,
});
});
}
function exit(reason) {
logger.log({ type: 'info', message: 'closing server', reason: reason });
if (server) server.close(process.exit.bind(process));
else process.exit();
}
}
start();