|
1 |
| -import contextlib |
2 |
| -import typing |
3 |
| - |
4 | 1 | import litestar
|
5 | 2 | import modern_di_litestar
|
6 | 3 | from advanced_alchemy.exceptions import DuplicateKeyError
|
|
12 | 9 | from app.settings import settings
|
13 | 10 |
|
14 | 11 |
|
15 |
| -class AppBuilder: |
16 |
| - def __init__(self) -> None: |
17 |
| - self.app: litestar.Litestar = litestar.Litestar( |
18 |
| - debug=settings.debug, |
19 |
| - lifespan=[self.lifespan_manager], |
20 |
| - exception_handlers={ |
21 |
| - DuplicateKeyError: exceptions.duplicate_key_error_handler, |
22 |
| - }, |
23 |
| - route_handlers=[ROUTER], |
24 |
| - dependencies={ |
25 |
| - **modern_di_litestar.prepare_di_dependencies(), |
26 |
| - "decks_service": modern_di_litestar.FromDI(ioc.Dependencies.decks_service), |
27 |
| - "cards_service": modern_di_litestar.FromDI(ioc.Dependencies.cards_service), |
28 |
| - }, |
29 |
| - openapi_config=OpenAPIConfig( |
30 |
| - title="Litestar Example", |
31 |
| - description="Example of Litestar with Scalar OpenAPI docs", |
32 |
| - version="0.0.1", |
33 |
| - render_plugins=[SwaggerRenderPlugin()], |
34 |
| - ), |
35 |
| - ) |
36 |
| - self.di_container = modern_di_litestar.setup_di(self.app) |
37 |
| - |
38 |
| - @contextlib.asynccontextmanager |
39 |
| - async def lifespan_manager(self, _: litestar.Litestar | None) -> typing.AsyncIterator[None]: |
40 |
| - async with self.di_container: |
41 |
| - await ioc.Dependencies.async_resolve_creators(self.di_container) |
42 |
| - yield |
| 12 | +def build_app() -> litestar.Litestar: |
| 13 | + return litestar.Litestar( |
| 14 | + debug=settings.debug, |
| 15 | + exception_handlers={ |
| 16 | + DuplicateKeyError: exceptions.duplicate_key_error_handler, |
| 17 | + }, |
| 18 | + route_handlers=[ROUTER], |
| 19 | + plugins=[modern_di_litestar.ModernDIPlugin()], |
| 20 | + dependencies={ |
| 21 | + "decks_service": modern_di_litestar.FromDI(ioc.Dependencies.decks_service), |
| 22 | + "cards_service": modern_di_litestar.FromDI(ioc.Dependencies.cards_service), |
| 23 | + }, |
| 24 | + openapi_config=OpenAPIConfig( |
| 25 | + title="Litestar Example", |
| 26 | + description="Example of Litestar with Scalar OpenAPI docs", |
| 27 | + version="0.0.1", |
| 28 | + render_plugins=[SwaggerRenderPlugin()], |
| 29 | + ), |
| 30 | + ) |
43 | 31 |
|
44 | 32 |
|
45 |
| -application = AppBuilder().app |
| 33 | +application = build_app() |
0 commit comments