-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfastify.ts
More file actions
77 lines (65 loc) · 2.26 KB
/
fastify.ts
File metadata and controls
77 lines (65 loc) · 2.26 KB
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
import Fastify, { FastifyServerOptions } from "fastify";
import cors from "@fastify/cors";
import rateLimit from "@fastify/rate-limit";
import rawBody from "fastify-raw-body";
import formbody from "@fastify/formbody";
import { yoga } from "./yoga";
import { githubRouter } from "./app/github/github.router";
import { errorHandler } from "./lib/fastify-helpers";
import { bullBoardRouter } from "./bull-mq/bull-board.router";
import { setupFastifyErrorHandler } from "./lib/sentry";
import { stripeRouter } from "./app/billing/stripe.router";
import { slackRouter } from "./app/integrations/slack/slack.router";
import { env } from "./env";
import { deploymentsRouter } from "./app/deployment/deployments.router";
import { healthRouter } from "./app/health/health.router";
import { isAppSelfHosted } from "./lib/self-host";
export async function buildApp(opts: FastifyServerOptions = {}) {
const app = Fastify(opts);
await app.register(cors, {
origin: env.FRONTEND_URL,
credentials: true,
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"],
});
await app.register(rawBody, {
field: "rawBody",
global: false,
encoding: "utf8",
runFirst: true,
});
await app.register(formbody);
// Allow Fastify to forward multipart requests to GraphQL Yoga
app.addContentTypeParser("multipart/form-data", {}, (_req, _payload, done) =>
done(null)
);
// Integration APIs
await app.register(healthRouter);
await app.register(githubRouter);
await app.register(slackRouter);
await app.register(bullBoardRouter);
if (!isAppSelfHosted()) {
await app.register(stripeRouter);
}
// User-facing APIs. All routes registered inside this scope are rate limited.
await app.register(async (scope) => {
await scope.register(rateLimit, {
max: 100,
timeWindow: "1 minute",
keyGenerator: (request) => request.headers.authorization || request.ip,
});
await scope.register(deploymentsRouter);
});
// GraphQL
app.route({
url: yoga.graphqlEndpoint,
method: ["GET", "POST", "OPTIONS"],
handler: (req, reply) =>
yoga.handleNodeRequestAndResponse(req, reply, {
req,
reply,
} as never),
});
setupFastifyErrorHandler(app);
app.setErrorHandler(errorHandler);
return app;
}