@@ -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,8 @@ import {
4347import { ZodOpenApiVersion } from "zod-openapi" ;
4448import { withTags } from "./components/index.js" ;
4549import apiKeyRoute from "./routes/apiKey.js" ;
50+ import RedisModule from "ioredis" ;
51+ import fastifyCron from "fastify-cron" ;
4652
4753dotenv . config ( ) ;
4854
@@ -56,6 +62,12 @@ async function init(prettyPrint: boolean = false) {
5662 const secretsManagerClient = new SecretsManagerClient ( {
5763 region : genericConfig . AwsRegion ,
5864 } ) ;
65+ const secret = ( await getSecretValue (
66+ secretsManagerClient ,
67+ genericConfig . ConfigSecretName ,
68+ ) ) as SecretConfig ;
69+ const redisClient = new RedisModule . default ( secret . redis_url ) ;
70+
5971 const transport = prettyPrint
6072 ? {
6173 target : "pino-pretty" ,
@@ -224,6 +236,26 @@ async function init(prettyPrint: boolean = false) {
224236 app . nodeCache = new NodeCache ( { checkperiod : 30 } ) ;
225237 app . dynamoClient = dynamoClient ;
226238 app . secretsManagerClient = secretsManagerClient ;
239+ app . redisClient = redisClient ;
240+ app . secretConfig = secret ;
241+ app . refreshSecretConfig = async ( ) => {
242+ app . secretConfig = ( await getSecretValue (
243+ app . secretsManagerClient ,
244+ genericConfig . ConfigSecretName ,
245+ ) ) as SecretConfig ;
246+ } ;
247+ app . register ( fastifyCron . default , {
248+ // refresh secrets config
249+ jobs : [
250+ {
251+ cronTime : "*/15 * * * *" ,
252+ onTick : async ( server ) => {
253+ server . log . info ( "Refreshing secrets manager config." ) ;
254+ await server . refreshSecretConfig ( ) ;
255+ } ,
256+ } ,
257+ ] ,
258+ } ) ;
227259 app . addHook ( "onRequest" , ( req , _ , done ) => {
228260 req . startTime = now ( ) ;
229261 const hostname = req . hostname ;
@@ -250,7 +282,13 @@ async function init(prettyPrint: boolean = false) {
250282 summary : "Verify that the API server is healthy." ,
251283 } ) ,
252284 } ,
253- ( _ , reply ) => reply . send ( { message : "UP" } ) ,
285+ async ( _ , reply ) => {
286+ const startTime = new Date ( ) . getTime ( ) ;
287+ await app . redisClient . ping ( ) ;
288+ const redisTime = new Date ( ) . getTime ( ) ;
289+ app . log . debug ( `Redis latency: ${ redisTime - startTime } ms.` ) ;
290+ return reply . send ( { message : "UP" } ) ;
291+ } ,
254292 ) ;
255293 await app . register (
256294 async ( api , _options ) => {
@@ -295,7 +333,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
295333 process . exit ( 1 ) ;
296334 }
297335 const app = await init ( true ) ;
298- app . listen ( { port : 8080 } , async ( err ) => {
336+ app . listen ( { port : 8080 } , ( err ) => {
299337 /* eslint no-console: ["error", {"allow": ["log", "error"]}] */
300338 if ( err ) {
301339 console . error ( err ) ;
0 commit comments