-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
103 lines (98 loc) · 3.61 KB
/
index.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
/* eslint import/no-nodejs-modules: ["error", {"allow": ["crypto"]}] */
import { randomUUID } from "crypto";
import fastify, { FastifyInstance } from "fastify";
import FastifyAuthProvider from "@fastify/auth";
import fastifyAuthPlugin from "./plugins/auth.js";
import protectedRoute from "./routes/protected.js";
import errorHandlerPlugin from "./plugins/errorHandler.js";
import { RunEnvironment, runEnvironments } from "../common/roles.js";
import { InternalServerError } from "../common/errors/index.js";
import eventsPlugin from "./routes/events.js";
import cors from "@fastify/cors";
import fastifyZodValidationPlugin from "./plugins/validate.js";
import { environmentConfig } from "../common/config.js";
import organizationsPlugin from "./routes/organizations.js";
import icalPlugin from "./routes/ics.js";
import vendingPlugin from "./routes/vending.js";
import * as dotenv from "dotenv";
import iamRoutes from "./routes/iam.js";
import ticketsPlugin from "./routes/tickets.js";
import membershipPlugin from "./routes/membership.js";
dotenv.config();
const now = () => Date.now();
async function init() {
const app: FastifyInstance = fastify({
logger: true,
disableRequestLogging: true,
genReqId: (request) => {
const header = request.headers["x-apigateway-event"];
if (!header) {
return randomUUID().toString();
}
const typeCheckedHeader = Array.isArray(header) ? header[0] : header;
const event = JSON.parse(decodeURIComponent(typeCheckedHeader));
return event.requestContext.requestId;
},
});
await app.register(fastifyAuthPlugin);
await app.register(fastifyZodValidationPlugin);
await app.register(FastifyAuthProvider);
await app.register(errorHandlerPlugin);
if (!process.env.RunEnvironment) {
process.env.RunEnvironment = "dev";
}
if (!runEnvironments.includes(process.env.RunEnvironment as RunEnvironment)) {
throw new InternalServerError({
message: `Invalid run environment ${app.runEnvironment}.`,
});
}
app.runEnvironment = process.env.RunEnvironment as RunEnvironment;
app.environmentConfig =
environmentConfig[app.runEnvironment as RunEnvironment];
app.addHook("onRequest", (req, _, done) => {
req.startTime = now();
req.log.info({ url: req.raw.url }, "received request");
done();
});
app.addHook("onResponse", (req, reply, done) => {
req.log.info(
{
url: req.raw.url,
statusCode: reply.raw.statusCode,
durationMs: now() - req.startTime,
},
"request completed",
);
done();
});
app.get("/api/v1/healthz", (_, reply) => reply.send({ message: "UP" }));
await app.register(
async (api, _options) => {
api.register(protectedRoute, { prefix: "/protected" });
api.register(eventsPlugin, { prefix: "/events" });
api.register(organizationsPlugin, { prefix: "/organizations" });
api.register(icalPlugin, { prefix: "/ical" });
api.register(iamRoutes, { prefix: "/iam" });
api.register(membershipPlugin, { prefix: "/membership" });
api.register(ticketsPlugin, { prefix: "/tickets" });
if (app.runEnvironment === "dev") {
api.register(vendingPlugin, { prefix: "/vending" });
}
},
{ prefix: "/api/v1" },
);
await app.register(cors, {
origin: app.environmentConfig.ValidCorsOrigins,
});
return app;
}
if (import.meta.url === `file://${process.argv[1]}`) {
// local development
const app = await init();
app.listen({ port: 8080 }, (err) => {
/* eslint no-console: ["error", {"allow": ["log", "error"]}] */
if (err) console.error(err);
console.log("Server listening on 8080");
});
}
export default init;