Skip to content

Commit

Permalink
Add CORS middleware BEFORE handing off request (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chennisden authored Jun 5, 2024
1 parent 554daaf commit acc0475
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions store/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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=["*"],
)

0 comments on commit acc0475

Please sign in to comment.