Skip to content

Commit 2821207

Browse files
authored
Add CORS to FastAPI app
1 parent e9cbf15 commit 2821207

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

app/application.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
import typing
33

44
import fastapi
5+
from fastapi.middleware.cors import CORSMiddleware
56
import modern_di_fastapi
67
from advanced_alchemy.exceptions import DuplicateKeyError
78

89
from app import exceptions, ioc
910
from app.api.decks import ROUTER
1011
from app.settings import settings
1112

13+
ALLOWED_ORIGINS = [
14+
"http://localhost:5173",
15+
# YOUR ALLOWED ORIGINS HERE
16+
]
1217

1318
def include_routers(app: fastapi.FastAPI) -> None:
1419
app.include_router(ROUTER, prefix="/api")
@@ -27,6 +32,13 @@ def __init__(self) -> None:
2732
DuplicateKeyError,
2833
exceptions.duplicate_key_error_handler, # type: ignore[arg-type]
2934
)
35+
self.app.add_middleware(
36+
CORSMiddleware,
37+
allow_origins=ALLOWED_ORIGINS,
38+
allow_credentials=True,
39+
allow_methods=["*"],
40+
allow_headers=["*"],
41+
)
3042

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

0 commit comments

Comments
 (0)