Skip to content

Commit

Permalink
Add CORS to FastAPI app (#34)
Browse files Browse the repository at this point in the history
* Add CORS to FastAPI app

* fix format

---------

Co-authored-by: Artur Shiriev <[email protected]>
  • Loading branch information
riyavsinha and lesnik512 authored Jan 11, 2025
1 parent e9cbf15 commit c4368c8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 58 deletions.
14 changes: 14 additions & 0 deletions app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import fastapi
import modern_di_fastapi
from advanced_alchemy.exceptions import DuplicateKeyError
from fastapi.middleware.cors import CORSMiddleware

from app import exceptions, ioc
from app.api.decks import ROUTER
from app.settings import settings


ALLOWED_ORIGINS = [
"http://localhost:5173",
# YOUR ALLOWED ORIGINS HERE
]


def include_routers(app: fastapi.FastAPI) -> None:
app.include_router(ROUTER, prefix="/api")

Expand All @@ -27,6 +34,13 @@ def __init__(self) -> None:
DuplicateKeyError,
exceptions.duplicate_key_error_handler, # type: ignore[arg-type]
)
self.app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

@contextlib.asynccontextmanager
async def lifespan_manager(self, _: fastapi.FastAPI) -> typing.AsyncIterator[dict[str, typing.Any]]:
Expand Down
Loading

0 comments on commit c4368c8

Please sign in to comment.