diff --git a/apps/api/v1/middleware.ts b/apps/api/v1/middleware.ts new file mode 100644 index 0000000000..126c6f7d1a --- /dev/null +++ b/apps/api/v1/middleware.ts @@ -0,0 +1,22 @@ +import { get } from "@vercel/edge-config"; +import { NextResponse } from "next/server"; + +const safeGet = async (key: string): Promise => { + try { + return get(key); + } catch (error) { + // Don't crash if EDGE_CONFIG env var is missing + } +}; + +export const config = { matcher: "/:path*" }; + +export async function middleware() { + const isInMaintenanceMode = await safeGet("isInMaintenanceMode"); + if (!isInMaintenanceMode) return NextResponse.next(); + // If is in maintenance mode, return a 503 status code + return NextResponse.json( + { message: "API is currently under maintenance. Please try again at a later time." }, + { status: 503 } + ); +}