-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathapplication.py
More file actions
41 lines (33 loc) · 1.24 KB
/
application.py
File metadata and controls
41 lines (33 loc) · 1.24 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
import dataclasses
import typing
import modern_di
import modern_di_fastapi
from advanced_alchemy.exceptions import DuplicateKeyError
from lite_bootstrap import FastAPIBootstrapper
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from app import exceptions, ioc
from app.api.decks import ROUTER
from app.settings import settings
if typing.TYPE_CHECKING:
import fastapi
def include_routers(app: fastapi.FastAPI) -> None:
app.include_router(ROUTER, prefix="/api")
def build_app() -> fastapi.FastAPI:
di_container = modern_di.Container(groups=[ioc.Dependencies])
bootstrap_config = dataclasses.replace(
settings.api_bootstrapper_config,
opentelemetry_instrumentors=[
SQLAlchemyInstrumentor(),
AsyncPGInstrumentor(capture_parameters=True),
],
)
bootstrapper = FastAPIBootstrapper(bootstrap_config=bootstrap_config)
app: fastapi.FastAPI = bootstrapper.bootstrap()
modern_di_fastapi.setup_di(app, di_container)
include_routers(app)
app.add_exception_handler(
DuplicateKeyError,
exceptions.duplicate_key_error_handler, # type: ignore[arg-type]
)
return app