From 177b47fc33228224445942efb2f25a8221c496cc Mon Sep 17 00:00:00 2001 From: farhanW3 Date: Wed, 10 Jan 2024 16:26:53 -0800 Subject: [PATCH] dynamic loading of cors urls --- src/server/middleware/cors/cors.ts | 8 +++++++- src/server/middleware/cors/index.ts | 6 ------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/server/middleware/cors/cors.ts b/src/server/middleware/cors/cors.ts index 62a25a36c..fea9bb190 100644 --- a/src/server/middleware/cors/cors.ts +++ b/src/server/middleware/cors/cors.ts @@ -4,6 +4,7 @@ import { FastifyRequest, HookHandlerDoneFunction, } from "fastify"; +import { getConfig } from "../../../utils/cache/getConfig"; import { addAccessControlRequestHeadersToVaryHeader, addOriginToVaryHeader, @@ -101,13 +102,18 @@ const defaultOptions = { strictPreflight: true, }; -export const fastifyCors = ( +export const fastifyCors = async ( fastify: FastifyInstance, req: FastifyRequest, reply: FastifyReply, opts: FastifyCorsOptions, next: HookHandlerDoneFunction, ) => { + const config = await getConfig(); + + const originArray = config.accessControlAllowOrigin.split(",") as string[]; + opts.origin = originArray; + let hideOptionsRoute = true; if (opts.hideOptionsRoute !== undefined) { hideOptionsRoute = opts.hideOptionsRoute; diff --git a/src/server/middleware/cors/index.ts b/src/server/middleware/cors/index.ts index afa46bbe1..0fb73bee6 100644 --- a/src/server/middleware/cors/index.ts +++ b/src/server/middleware/cors/index.ts @@ -1,19 +1,13 @@ import { FastifyInstance } from "fastify"; -import { getConfig } from "../../../utils/cache/getConfig"; import { fastifyCors } from "./cors"; export const withCors = async (server: FastifyInstance) => { - const config = await getConfig(); - - const originArray = config.accessControlAllowOrigin.split(",") as string[]; - server.addHook("preHandler", (request, reply, next) => { fastifyCors( server, request, reply, { - origin: originArray, credentials: true, }, next,