|
1 | | -from collections.abc import AsyncIterator, Iterable |
| 1 | +from collections.abc import AsyncIterator |
2 | 2 | from contextlib import asynccontextmanager |
3 | 3 |
|
4 | 4 | from dishka import AsyncContainer, Provider, make_async_container |
5 | | -from fastapi import APIRouter, FastAPI |
| 5 | +from fastapi import FastAPI |
6 | 6 | from fastapi.responses import ORJSONResponse |
7 | 7 |
|
8 | 8 | from app.infrastructure.persistence_sqla.mappings.all import map_tables |
9 | 9 | from app.presentation.http.auth.asgi_middleware import ( |
10 | 10 | ASGIAuthMiddleware, |
11 | 11 | ) |
| 12 | +from app.presentation.http.controllers.root_router import create_root_router |
12 | 13 | from app.setup.config.settings import AppSettings |
| 14 | +from app.setup.ioc.provider_registry import get_providers |
13 | 15 |
|
14 | 16 |
|
15 | | -def create_app() -> FastAPI: |
16 | | - return FastAPI( |
| 17 | +def create_ioc_container( |
| 18 | + settings: AppSettings, |
| 19 | + *di_providers: Provider, |
| 20 | +) -> AsyncContainer: |
| 21 | + return make_async_container( |
| 22 | + *get_providers(), |
| 23 | + *di_providers, |
| 24 | + context={AppSettings: settings}, |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def create_web_app() -> FastAPI: |
| 29 | + app = FastAPI( |
17 | 30 | lifespan=lifespan, |
18 | 31 | default_response_class=ORJSONResponse, |
19 | 32 | ) |
| 33 | + # https://github.com/encode/starlette/discussions/2451 |
| 34 | + app.add_middleware(ASGIAuthMiddleware) |
| 35 | + # Good place to register global exception handlers |
| 36 | + app.include_router(create_root_router()) |
| 37 | + return app |
20 | 38 |
|
21 | 39 |
|
22 | 40 | @asynccontextmanager |
23 | 41 | async def lifespan(app: FastAPI) -> AsyncIterator[None]: |
24 | 42 | map_tables() |
25 | 43 | yield None |
26 | | - await app.state.dishka_container.close() |
27 | 44 | # https://dishka.readthedocs.io/en/stable/integrations/fastapi.html |
28 | | - |
29 | | - |
30 | | -def configure_app( |
31 | | - app: FastAPI, |
32 | | - root_router: APIRouter, |
33 | | -) -> None: |
34 | | - app.include_router(root_router) |
35 | | - app.add_middleware(ASGIAuthMiddleware) |
36 | | - # https://github.com/encode/starlette/discussions/2451 |
37 | | - |
38 | | - # Good place to register global exception handlers |
39 | | - |
40 | | - |
41 | | -def create_async_ioc_container( |
42 | | - providers: Iterable[Provider], |
43 | | - settings: AppSettings, |
44 | | -) -> AsyncContainer: |
45 | | - return make_async_container( |
46 | | - *providers, |
47 | | - context={AppSettings: settings}, |
48 | | - ) |
| 45 | + await app.state.dishka_container.close() |
0 commit comments