-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapplication.py
52 lines (41 loc) · 1.45 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import contextlib
import typing
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")
class AppBuilder:
def __init__(self) -> None:
self.app: fastapi.FastAPI = fastapi.FastAPI(
title=settings.service_name,
debug=settings.debug,
lifespan=self.lifespan_manager,
)
self.di_container = modern_di_fastapi.setup_di(self.app)
include_routers(self.app)
self.app.add_exception_handler(
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]]:
async with self.di_container:
await ioc.Dependencies.async_resolve_creators(self.di_container)
yield {}
application = AppBuilder().app