-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (75 loc) · 2.22 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
88
const newrelic = require('newrelic');
const express = require('express');
const morgan = require('morgan');
const compression = require('compression');
const cors = require('cors');
const Rollbar = require('rollbar');
const { path } = require('ramda');
const queue = require('./routes-queue');
const webhook = require('./routes-webhook');
const pullRequest = require('./routes-pullrequest');
const analytics = require('./analytics');
const logger = require('./logger');
const {
env: {
ROLLBAR_KEY: rollbarAccessToken,
PORT: port = 3000,
APP_ORIGIN: appOrigin = 'http://localhost:4000',
UA: analyticsUa,
},
} = process;
require('./handler-event-queue');
require('./handler-notification-push');
require('./handler-notification-pusher');
require('./manager-metrics');
const app = express();
const environment = app.get('env') || 'production';
const development = environment === 'development';
const test = environment === 'test';
const rollbar = new Rollbar({
accessToken: rollbarAccessToken,
environment: environment,
captureUncaught: !development,
captureUnhandledRejections: !development,
});
app.use(express.json());
app.use(compression());
app.use(cors({ origin: appOrigin }));
morgan.token('remote-user', path(['user', 'username']));
if (analyticsUa) {
app.use(analytics.trackRequest);
}
app.use('/queue', queue);
app.use('/pull-request', pullRequest);
app.use('/webhook', webhook);
app.disable('x-powered-by');
app.disable('etag');
app.enable('trust proxy');
app.use((req, res, next) => {
newrelic.addCustomAttribute('username', path(['user', 'username'], req));
next();
});
// error handlers
if (!development) {
app.use(rollbar.errorHandler());
}
app.use((err, req, res, next) => {
const status = err.status || 500;
if (status >= 500 && !development) {
rollbar.error(err, req);
}
if (development) {
logger.error(err.stack);
}
res.status(status).json({
stack: development ? err.stack : undefined,
error: err.message,
});
});
if (!test) {
app.use(morgan('combined', { stream: { write: message => logger.info(message.trim()) } }));
}
app.get('/', (req, res) => {
res.send('');
});
module.exports = app.listen(port);