Skip to content

Commit

Permalink
dynamic loading of cors urls
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanW3 committed Jan 11, 2024
1 parent ada171e commit 177b47f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/server/middleware/cors/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FastifyRequest,
HookHandlerDoneFunction,
} from "fastify";
import { getConfig } from "../../../utils/cache/getConfig";
import {
addAccessControlRequestHeadersToVaryHeader,
addOriginToVaryHeader,
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/server/middleware/cors/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 177b47f

Please sign in to comment.