|
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 | | -import sigleadRoutes from "./routes/siglead.js"; |
29 | 15 | import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts"; |
30 | 16 | import NodeCache from "node-cache"; |
31 | 17 | import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
32 | 18 | import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager"; |
33 | | -import mobileWalletRoute from "./routes/mobileWallet.js"; |
34 | | -import stripeRoutes from "./routes/stripe.js"; |
35 | | -import membershipPlugin from "./routes/membership.js"; |
36 | | -import path from "path"; // eslint-disable-line import/no-nodejs-modules |
37 | | -import roomRequestRoutes from "./routes/roomRequests.js"; |
38 | | -import logsPlugin from "./routes/logs.js"; |
39 | | - |
40 | 19 | import { |
41 | 20 | fastifyZodOpenApiPlugin, |
42 | 21 | fastifyZodOpenApiTransform, |
43 | 22 | fastifyZodOpenApiTransformObject, |
44 | 23 | serializerCompiler, |
45 | 24 | validatorCompiler, |
46 | 25 | } from "fastify-zod-openapi"; |
47 | | -import { ZodOpenApiVersion } from "zod-openapi"; |
| 26 | +import { type ZodOpenApiVersion } from "zod-openapi"; |
48 | 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"; |
49 | 57 | import apiKeyRoute from "./routes/apiKey.js"; |
50 | 58 | import clearSessionRoute from "./routes/clearSession.js"; |
51 | | -import RedisModule from "ioredis"; |
52 | | -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 | +import sigleadRoutes from "./routes/siglead.js"; |
| 62 | +/** END ROUTES */ |
| 63 | + |
53 | 64 | const __filename = fileURLToPath(import.meta.url); |
54 | 65 | const __dirname = path.dirname(__filename); |
55 | 66 |
|
56 | 67 | dotenv.config(); |
57 | 68 |
|
58 | 69 | const now = () => Date.now(); |
| 70 | +const isRunningInLambda = |
| 71 | + process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME; |
59 | 72 |
|
60 | 73 | async function init(prettyPrint: boolean = false, initClients: boolean = true) { |
61 | | - const isRunningInLambda = |
62 | | - process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME; |
63 | 74 | let isSwaggerServer = false; |
64 | 75 | const transport = prettyPrint |
65 | 76 | ? { |
@@ -96,6 +107,7 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) { |
96 | 107 | await app.register(evaluatePoliciesPlugin); |
97 | 108 | await app.register(errorHandlerPlugin); |
98 | 109 | await app.register(fastifyZodOpenApiPlugin); |
| 110 | + await app.register(locationPlugin); |
99 | 111 | if (!isRunningInLambda) { |
100 | 112 | try { |
101 | 113 | const fastifySwagger = import("@fastify/swagger"); |
@@ -279,6 +291,14 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) { |
279 | 291 | await app.refreshSecretConfig(); |
280 | 292 | app.redisClient = new RedisModule.default(app.secretConfig.redis_url); |
281 | 293 | } |
| 294 | + if (isRunningInLambda) { |
| 295 | + await app.register(fastifyIp.default, { |
| 296 | + order: ["x-forwarded-for"], |
| 297 | + strict: true, |
| 298 | + isAWS: false, |
| 299 | + }); |
| 300 | + } |
| 301 | + |
282 | 302 | app.addHook("onRequest", (req, _, done) => { |
283 | 303 | req.startTime = now(); |
284 | 304 | const hostname = req.hostname; |
@@ -339,6 +359,7 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) { |
339 | 359 | origin: app.environmentConfig.ValidCorsOrigins, |
340 | 360 | methods: ["GET", "HEAD", "POST", "PATCH", "DELETE"], |
341 | 361 | }); |
| 362 | + |
342 | 363 | app.addHook("onSend", async (request, reply) => { |
343 | 364 | reply.header("X-Request-Id", request.id); |
344 | 365 | }); |
|
0 commit comments