-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathapp_factory.py
More file actions
45 lines (37 loc) · 1.35 KB
/
app_factory.py
File metadata and controls
45 lines (37 loc) · 1.35 KB
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
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from dishka import AsyncContainer, Provider, make_async_container
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse
from app.infrastructure.persistence_sqla.mappings.all import map_tables
from app.presentation.http.auth.asgi_middleware import (
ASGIAuthMiddleware,
)
from app.presentation.http.controllers.root_router import create_root_router
from app.setup.config.settings import AppSettings
from app.setup.ioc.provider_registry import get_providers
def create_ioc_container(
settings: AppSettings,
*di_providers: Provider,
) -> AsyncContainer:
return make_async_container(
*get_providers(),
*di_providers,
context={AppSettings: settings},
)
def create_web_app() -> FastAPI:
app = FastAPI(
lifespan=lifespan,
default_response_class=ORJSONResponse,
)
# https://github.com/encode/starlette/discussions/2451
app.add_middleware(ASGIAuthMiddleware)
# Good place to register global exception handlers
app.include_router(create_root_router())
return app
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
map_tables()
yield None
# https://dishka.readthedocs.io/en/stable/integrations/fastapi.html
await app.state.dishka_container.close()