-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (61 loc) · 1.56 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
const http = require('http');
const Koa = require('koa');
const config = require('config');
const Sentry = require('@sentry/node');
require('dnscache')(config.dnscache);
const serviceHeaders = require('ria-service-middleware');
const k8s = require('@bu-auto-modules/k8s-probes');
Sentry.init({
environment: `server-${config.app.env}`,
dsn: config.sentry.dsn,
});
const {
routesPublic,
routesPrivate,
} = require('./server/routes/index');
const
app = new Koa();
app.use(serviceHeaders);
app.use(async (ctx, next) => {
if (process.env.TEST_USER) {
ctx.userId = process.env.TEST_USER;
}
await next();
});
app.use(routesPrivate());
app.use(routesPublic());
app.use(async (ctx, next) => {
if (ctx.status >= 500) {
const error = new Error();
error.message = `Status->${ctx.status} Method->${ctx.method} headers-> ${JSON.stringify(ctx.request.headers)}`;
console.error(error);
}
await next();
});
app.on('error', (err, ctx) => {
if (!ctx.status || ctx.status >= 500) {
Sentry.setUser({
id: ctx.userId,
});
Sentry.withScope((scope) => {
scope.addEventProcessor((event) => Sentry.Handlers.parseRequest(event, ctx.request));
Sentry.captureException(err);
});
}
});
k8s.ready = true;
const server = http.createServer(app.callback())
.listen(config.server.port, () => {
// eslint-disable-next-line no-console
console.table({
Application: config.app.name,
Version: config.app.version,
Environment: config.app.env,
});
});
module.exports = {
server,
closeServer() {
server.close();
},
};