|
1 |
| -/* eslint import/no-nodejs-modules: ["error", {"allow": ["crypto"]}] */ |
| 1 | +/* eslint import/no-nodejs-modules: ["error", {"allow": ["crypto", "path", "url"]}] */ |
2 | 2 |
|
3 | 3 | import { randomUUID } from "crypto";
|
| 4 | +import { fileURLToPath } from "url"; |
| 5 | +import path from "path"; |
4 | 6 | import fastify, { FastifyInstance } from "fastify";
|
5 |
| -import FastifyAuthProvider from "@fastify/auth"; |
6 |
| -import fastifyStatic from "@fastify/static"; |
7 |
| -import fastifyAuthPlugin, { getSecretValue } from "./plugins/auth.js"; |
8 |
| -import protectedRoute from "./routes/protected.js"; |
9 |
| -import errorHandlerPlugin from "./plugins/errorHandler.js"; |
10 | 7 | import { RunEnvironment, runEnvironments } from "../common/roles.js";
|
11 | 8 | import { InternalServerError } from "../common/errors/index.js";
|
12 |
| -import eventsPlugin from "./routes/events.js"; |
13 |
| -import cors from "@fastify/cors"; |
14 | 9 | import {
|
15 | 10 | environmentConfig,
|
16 | 11 | genericConfig,
|
17 | 12 | SecretConfig,
|
18 | 13 | } from "../common/config.js";
|
19 |
| -import organizationsPlugin from "./routes/organizations.js"; |
20 |
| -import authorizeFromSchemaPlugin from "./plugins/authorizeFromSchema.js"; |
21 |
| -import evaluatePoliciesPlugin from "./plugins/evaluatePolicies.js"; |
22 |
| -import icalPlugin from "./routes/ics.js"; |
23 |
| -import vendingPlugin from "./routes/vending.js"; |
24 | 14 | import * as dotenv from "dotenv";
|
25 |
| -import iamRoutes from "./routes/iam.js"; |
26 |
| -import ticketsPlugin from "./routes/tickets.js"; |
27 |
| -import linkryRoutes from "./routes/linkry.js"; |
28 | 15 | import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
|
29 | 16 | import NodeCache from "node-cache";
|
30 | 17 | import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
31 | 18 | import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
|
32 |
| -import mobileWalletRoute from "./routes/mobileWallet.js"; |
33 |
| -import stripeRoutes from "./routes/stripe.js"; |
34 |
| -import membershipPlugin from "./routes/membership.js"; |
35 |
| -import path from "path"; // eslint-disable-line import/no-nodejs-modules |
36 |
| -import roomRequestRoutes from "./routes/roomRequests.js"; |
37 |
| -import logsPlugin from "./routes/logs.js"; |
38 |
| - |
39 | 19 | import {
|
40 | 20 | fastifyZodOpenApiPlugin,
|
41 | 21 | fastifyZodOpenApiTransform,
|
42 | 22 | fastifyZodOpenApiTransformObject,
|
43 | 23 | serializerCompiler,
|
44 | 24 | validatorCompiler,
|
45 | 25 | } from "fastify-zod-openapi";
|
46 |
| -import { ZodOpenApiVersion } from "zod-openapi"; |
| 26 | +import { type ZodOpenApiVersion } from "zod-openapi"; |
47 | 27 | import { withTags } from "./components/index.js";
|
| 28 | +import RedisModule from "ioredis"; |
| 29 | + |
| 30 | +/** BEGIN EXTERNAL PLUGINS */ |
| 31 | +import fastifyIp from "fastify-ip"; |
| 32 | +import cors from "@fastify/cors"; |
| 33 | +import FastifyAuthProvider from "@fastify/auth"; |
| 34 | +import fastifyStatic from "@fastify/static"; |
| 35 | +/** END EXTERNAL PLUGINS */ |
| 36 | + |
| 37 | +/** BEGIN INTERNAL PLUGINS */ |
| 38 | +import locationPlugin from "./plugins/location.js"; |
| 39 | +import fastifyAuthPlugin, { getSecretValue } from "./plugins/auth.js"; |
| 40 | +import errorHandlerPlugin from "./plugins/errorHandler.js"; |
| 41 | +import authorizeFromSchemaPlugin from "./plugins/authorizeFromSchema.js"; |
| 42 | +import evaluatePoliciesPlugin from "./plugins/evaluatePolicies.js"; |
| 43 | +/** END INTERNAL PLUGINS */ |
| 44 | + |
| 45 | +/** BEGIN ROUTES */ |
| 46 | +import organizationsPlugin from "./routes/organizations.js"; |
| 47 | +import icalPlugin from "./routes/ics.js"; |
| 48 | +import vendingPlugin from "./routes/vending.js"; |
| 49 | +import iamRoutes from "./routes/iam.js"; |
| 50 | +import ticketsPlugin from "./routes/tickets.js"; |
| 51 | +import linkryRoutes from "./routes/linkry.js"; |
| 52 | +import mobileWalletRoute from "./routes/mobileWallet.js"; |
| 53 | +import stripeRoutes from "./routes/stripe.js"; |
| 54 | +import membershipPlugin from "./routes/membership.js"; |
| 55 | +import roomRequestRoutes from "./routes/roomRequests.js"; |
| 56 | +import logsPlugin from "./routes/logs.js"; |
48 | 57 | import apiKeyRoute from "./routes/apiKey.js";
|
49 | 58 | import clearSessionRoute from "./routes/clearSession.js";
|
50 |
| -import RedisModule from "ioredis"; |
51 |
| -import { fileURLToPath } from "url"; // eslint-disable-line import/no-nodejs-modules |
| 59 | +import protectedRoute from "./routes/protected.js"; |
| 60 | +import eventsPlugin from "./routes/events.js"; |
| 61 | +/** END ROUTES */ |
| 62 | + |
52 | 63 | const __filename = fileURLToPath(import.meta.url);
|
53 | 64 | const __dirname = path.dirname(__filename);
|
54 | 65 |
|
55 | 66 | dotenv.config();
|
56 | 67 |
|
57 | 68 | const now = () => Date.now();
|
| 69 | +const isRunningInLambda = |
| 70 | + process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME; |
58 | 71 |
|
59 | 72 | async function init(prettyPrint: boolean = false, initClients: boolean = true) {
|
60 |
| - const isRunningInLambda = |
61 |
| - process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME; |
62 | 73 | let isSwaggerServer = false;
|
63 | 74 | const transport = prettyPrint
|
64 | 75 | ? {
|
@@ -95,6 +106,7 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) {
|
95 | 106 | await app.register(evaluatePoliciesPlugin);
|
96 | 107 | await app.register(errorHandlerPlugin);
|
97 | 108 | await app.register(fastifyZodOpenApiPlugin);
|
| 109 | + await app.register(locationPlugin); |
98 | 110 | if (!isRunningInLambda) {
|
99 | 111 | try {
|
100 | 112 | const fastifySwagger = import("@fastify/swagger");
|
@@ -278,6 +290,14 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) {
|
278 | 290 | await app.refreshSecretConfig();
|
279 | 291 | app.redisClient = new RedisModule.default(app.secretConfig.redis_url);
|
280 | 292 | }
|
| 293 | + if (isRunningInLambda) { |
| 294 | + await app.register(fastifyIp.default, { |
| 295 | + order: ["x-forwarded-for"], |
| 296 | + strict: true, |
| 297 | + isAWS: false, |
| 298 | + }); |
| 299 | + } |
| 300 | + |
281 | 301 | app.addHook("onRequest", (req, _, done) => {
|
282 | 302 | req.startTime = now();
|
283 | 303 | const hostname = req.hostname;
|
@@ -337,6 +357,7 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) {
|
337 | 357 | origin: app.environmentConfig.ValidCorsOrigins,
|
338 | 358 | methods: ["GET", "HEAD", "POST", "PATCH", "DELETE"],
|
339 | 359 | });
|
| 360 | + |
340 | 361 | app.addHook("onSend", async (request, reply) => {
|
341 | 362 | reply.header("X-Request-Id", request.id);
|
342 | 363 | });
|
|
0 commit comments