From acc0475613c00a159b2dc49d8258c258d2459c9b Mon Sep 17 00:00:00 2001 From: Dennis Chen <41879777+chennisden@users.noreply.github.com> Date: Tue, 4 Jun 2024 17:20:34 -0700 Subject: [PATCH] Add CORS middleware BEFORE handing off request (#50) * Add CORS middleware BEFORE handing off request This prevented false CORS errors that arose really because the server raised an exception and trying to immediately return a response (preventing the CORS middleware from being added in the first place) * fix import --- store/app/main.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/store/app/main.py b/store/app/main.py index 6977e4be..01e06268 100644 --- a/store/app/main.py +++ b/store/app/main.py @@ -12,6 +12,15 @@ app = FastAPI() +# Adds CORS middleware. +app.add_middleware( + CORSMiddleware, + allow_origins=[settings.site.homepage], + allow_credentials=True, + allow_methods=["GET", "POST", "DELETE", "OPTIONS"], + allow_headers=["*"], +) + FRONTEND_DIR = (Path(__file__).parent / "frontend").resolve() if not (FRONTEND_BUILD_DIR := FRONTEND_DIR / "build").exists(): raise FileNotFoundError(f"Frontend is not built to {FRONTEND_BUILD_DIR}") @@ -47,13 +56,3 @@ async def redirect_to_index(full_path: str, request: Request) -> Response: if full_path in FRONTEND_OTHER_FILES: return await StaticFiles(directory=FRONTEND_BUILD_DIR).get_response(full_path, request.scope) return await StaticFiles(directory=FRONTEND_BUILD_DIR).get_response("index.html", request.scope) - - -# Adds CORS middleware. -app.add_middleware( - CORSMiddleware, - allow_origins=[settings.site.homepage], - allow_credentials=True, - allow_methods=["GET", "POST", "DELETE", "OPTIONS"], - allow_headers=["*"], -)