-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
47 lines (38 loc) · 1.07 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
'use strict';
const HTTP = require('http');
const Koa = require('koa');
const koaSSL = require('koa-ssl');
const Logger = require('./lib/logger');
const Sentry = require('./services/sentry');
const shareDBWsServer = require('./sharedb');
const app = new Koa();
const server = HTTP.createServer();
if (process.env.NODE_ENV === 'production') {
app.use(koaSSL({ trustProxy: true }));
}
app.use(function* catchError(next) {
try {
yield next;
} catch (err) {
Logger.error(err);
Sentry.captureRequestException(this.req, err);
this.body =
{ id: 'unexpected', message: 'An unexpected message occurred.' };
}
});
app.use(function* (next) {
if (this.method === 'GET' && this.path === '/health') {
this.body = 'ok';
return;
}
if (this.method === 'GET' && this.path === '/boom') {
throw new Error('boom');
}
yield next;
});
shareDBWsServer(server);
server.on('request', app.callback());
server.listen(process.env.PORT, onServerListen);
function onServerListen() {
Logger.log({ event: `listening on port ${this.address().port}` });
}