-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildServer.ts
106 lines (91 loc) · 3.06 KB
/
buildServer.ts
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
import { createNodeMiddleware } from '@octokit/webhooks';
import { RewriteFrames } from '@sentry/integrations';
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import { routeHandlers } from '@webhooks';
import fastify from 'fastify';
import fastifyFormBody from 'fastify-formbody';
import middie from 'middie';
import { Fastify } from '@types';
import { githubEvents } from '@api/github';
import { bolt } from '@api/slack';
import { loadBrain } from '@utils/loadBrain';
import { SENTRY_DSN } from './config';
import { routeJobs } from './jobs';
import { SlackRouter } from './slack';
export async function buildServer(
logger: boolean | { prettyPrint: boolean } = {
prettyPrint: process.env.NODE_ENV === 'development',
}
) {
const server: Fastify = fastify({
logger,
disableRequestLogging: true,
});
await server.register(middie);
await server.register(fastifyFormBody);
Sentry.init({
dsn: SENTRY_DSN,
release: process.env.VERSION,
environment: process.env.ENV || 'development',
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Postgres(),
new RewriteFrames({ root: process.cwd() }),
],
tracesSampleRate: 1.0,
normalizeDepth: 6,
});
// For Sentry Release Health
// https://docs.sentry.io/platforms/node/configuration/releases/
server.use(Sentry.Handlers.requestHandler());
server.setErrorHandler((error, request, reply) => {
// Log to console when not in production environment
if (process.env.ENV !== 'production') {
console.error(error);
}
Sentry.captureException(error);
reply.code(500).send();
});
server.setNotFoundHandler(
// Note: The preValidation hook registered using this method will run for a
// route that Fastify does not recognize and not when a route handler
// manually calls reply.callNotFound. In which case, only preHandler will be
// run.
{
preValidation: (req, reply, done) => {
done();
},
preHandler: (req, reply, done) => {
done();
},
},
function (request, reply) {
// Default not found handler with preValidation and preHandler hooks
reply.code(404).type('text/html').send('Not Found');
}
);
server.get('/', {}, async (_request, _reply) => {
return '';
});
// Install Slack and GitHub handlers. Both the Bolt and @octokit/webhooks
// libraries operate as middleware that emit events corresponding to webhook
// POSTs. Our event handlers for both are under loadBrain.
// @ts-expect-error
server.use('/apps/slack/events', bolt.receiver.requestListener);
server.use(
'/webhooks/github',
createNodeMiddleware(githubEvents, { path: '/' })
);
await loadBrain();
// Other webhooks operate as regular Fastify handlers
server.register(routeHandlers);
// Endpoints for Cloud Scheduler webhooks (Cron Jobs)
server.register(routeJobs, { prefix: '/jobs' });
server.post<{ Params: { service: string } }>(
'/slack/:service/webhook',
{},
SlackRouter(server)
);
return server;
}