@@ -4,14 +4,18 @@ import { randomUUID } from "crypto";
44import fastify , { FastifyInstance } from "fastify" ;
55import FastifyAuthProvider from "@fastify/auth" ;
66import fastifyStatic from "@fastify/static" ;
7- import fastifyAuthPlugin from "./plugins/auth.js" ;
7+ import fastifyAuthPlugin , { getSecretValue } from "./plugins/auth.js" ;
88import protectedRoute from "./routes/protected.js" ;
99import errorHandlerPlugin from "./plugins/errorHandler.js" ;
1010import { RunEnvironment , runEnvironments } from "../common/roles.js" ;
1111import { InternalServerError } from "../common/errors/index.js" ;
1212import eventsPlugin from "./routes/events.js" ;
1313import cors from "@fastify/cors" ;
14- import { environmentConfig , genericConfig } from "../common/config.js" ;
14+ import {
15+ environmentConfig ,
16+ genericConfig ,
17+ SecretConfig ,
18+ } from "../common/config.js" ;
1519import organizationsPlugin from "./routes/organizations.js" ;
1620import authorizeFromSchemaPlugin from "./plugins/authorizeFromSchema.js" ;
1721import evaluatePoliciesPlugin from "./plugins/evaluatePolicies.js" ;
@@ -43,6 +47,7 @@ import {
4347import { ZodOpenApiVersion } from "zod-openapi" ;
4448import { withTags } from "./components/index.js" ;
4549import apiKeyRoute from "./routes/apiKey.js" ;
50+ import RedisModule from "ioredis" ;
4651
4752dotenv . config ( ) ;
4853
@@ -56,6 +61,12 @@ async function init(prettyPrint: boolean = false) {
5661 const secretsManagerClient = new SecretsManagerClient ( {
5762 region : genericConfig . AwsRegion ,
5863 } ) ;
64+ const secret = ( await getSecretValue (
65+ secretsManagerClient ,
66+ genericConfig . ConfigSecretName ,
67+ ) ) as SecretConfig ;
68+ const redisClient = new RedisModule . default ( secret . redis_url ) ;
69+
5970 const transport = prettyPrint
6071 ? {
6172 target : "pino-pretty" ,
@@ -224,6 +235,14 @@ async function init(prettyPrint: boolean = false) {
224235 app . nodeCache = new NodeCache ( { checkperiod : 30 } ) ;
225236 app . dynamoClient = dynamoClient ;
226237 app . secretsManagerClient = secretsManagerClient ;
238+ app . redisClient = redisClient ;
239+ app . secretConfig = secret ;
240+ app . refreshSecretConfig = async ( ) => {
241+ app . secretConfig = ( await getSecretValue (
242+ app . secretsManagerClient ,
243+ genericConfig . ConfigSecretName ,
244+ ) ) as SecretConfig ;
245+ } ;
227246 app . addHook ( "onRequest" , ( req , _ , done ) => {
228247 req . startTime = now ( ) ;
229248 const hostname = req . hostname ;
@@ -250,7 +269,13 @@ async function init(prettyPrint: boolean = false) {
250269 summary : "Verify that the API server is healthy." ,
251270 } ) ,
252271 } ,
253- ( _ , reply ) => reply . send ( { message : "UP" } ) ,
272+ async ( _ , reply ) => {
273+ const startTime = new Date ( ) . getTime ( ) ;
274+ await app . redisClient . ping ( ) ;
275+ const redisTime = new Date ( ) . getTime ( ) ;
276+ app . log . debug ( `Redis latency: ${ redisTime - startTime } ms.` ) ;
277+ return reply . send ( { message : "UP" } ) ;
278+ } ,
254279 ) ;
255280 await app . register (
256281 async ( api , _options ) => {
@@ -295,7 +320,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
295320 process . exit ( 1 ) ;
296321 }
297322 const app = await init ( true ) ;
298- app . listen ( { port : 8080 } , async ( err ) => {
323+ app . listen ( { port : 8080 } , ( err ) => {
299324 /* eslint no-console: ["error", {"allow": ["log", "error"]}] */
300325 if ( err ) {
301326 console . error ( err ) ;
0 commit comments