11import logging
2+ from collections .abc import AsyncIterator
23
34from fastapi import FastAPI
45from fastapi .middleware .gzip import GZipMiddleware
6+ from fastapi_lifespan_manager import LifespanManager , State
57from models_library .basic_types import BootModeEnum
68from servicelib .fastapi import timing_middleware
79from servicelib .fastapi .openapi import override_fastapi_openapi_method
10+ from servicelib .fastapi .postgres_lifespan import (
11+ PostgresLifespanStateKeys ,
12+ postgres_lifespan ,
13+ )
814from servicelib .fastapi .profiler import initialize_profiler
915from servicelib .fastapi .prometheus_instrumentation import (
1016 setup_prometheus_instrumentation ,
1117)
1218from servicelib .fastapi .tracing import initialize_tracing
1319from starlette .middleware .base import BaseHTTPMiddleware
1420
15- from .._meta import API_VERSION , API_VTAG , APP_NAME , PROJECT_NAME , SUMMARY
21+ from .._meta import (
22+ API_VERSION ,
23+ API_VTAG ,
24+ APP_NAME ,
25+ PROJECT_NAME ,
26+ SUMMARY ,
27+ )
1628from ..api .rest .routes import setup_rest_api_routes
1729from ..api .rpc .routes import setup_rpc_api_routes
30+ from ..db .events import setup_database
1831from ..exceptions .handlers import setup_exception_handlers
1932from ..services .function_services import setup_function_services
2033from ..services .rabbitmq import setup_rabbitmq
21- from .events import create_on_shutdown , create_on_startup
34+ from .events import (
35+ create_on_shutdown ,
36+ create_on_startup ,
37+ flush_finished_banner ,
38+ flush_started_banner ,
39+ )
2240from .settings import ApplicationSettings
2341
2442_logger = logging .getLogger (__name__ )
3452)
3553
3654
37- def create_app (settings : ApplicationSettings | None = None ) -> FastAPI :
55+ async def _main_setup (app : FastAPI ) -> AsyncIterator [State ]:
56+ flush_started_banner ()
57+
58+ settings : ApplicationSettings = app .state .settings
59+
60+ yield {
61+ PostgresLifespanStateKeys .POSTGRES_SETTINGS : settings .CATALOG_POSTGRES ,
62+ }
63+
64+ flush_finished_banner ()
65+
66+
67+ def _create_app_lifespan ():
68+ # app lifespan
69+ app_lifespan = LifespanManager ()
70+ app_lifespan .add (_main_setup )
71+
72+ # - postgres lifespan
73+ postgres_lifespan .add (setup_database )
74+ app_lifespan .include (postgres_lifespan )
75+
76+ return app_lifespan
77+
78+
79+ def create_app () -> FastAPI :
3880 # keep mostly quiet noisy loggers
3981 quiet_level : int = max (
4082 min (logging .root .level + _LOG_LEVEL_STEP , logging .CRITICAL ), logging .WARNING
4183 )
4284 for name in _NOISY_LOGGERS :
4385 logging .getLogger (name ).setLevel (quiet_level )
4486
45- if settings is None :
46- settings = ApplicationSettings .create_from_envs ()
47-
48- assert settings # nosec
87+ settings = ApplicationSettings .create_from_envs ()
4988 _logger .debug (settings .model_dump_json (indent = 2 ))
5089
5190 app = FastAPI (
@@ -57,6 +96,7 @@ def create_app(settings: ApplicationSettings | None = None) -> FastAPI:
5796 openapi_url = f"/api/{ API_VTAG } /openapi.json" ,
5897 docs_url = "/dev/doc" ,
5998 redoc_url = None , # default disabled
99+ lifespan = _create_app_lifespan (),
60100 )
61101 override_fastapi_openapi_method (app )
62102
@@ -73,11 +113,11 @@ def create_app(settings: ApplicationSettings | None = None) -> FastAPI:
73113 setup_function_services (app )
74114 setup_rabbitmq (app )
75115
76- if app . state . settings .CATALOG_PROMETHEUS_INSTRUMENTATION_ENABLED :
116+ if settings .CATALOG_PROMETHEUS_INSTRUMENTATION_ENABLED :
77117 setup_prometheus_instrumentation (app )
78118
79119 # MIDDLEWARES
80- if app . state . settings .CATALOG_PROFILING :
120+ if settings .CATALOG_PROFILING :
81121 initialize_profiler (app )
82122
83123 if settings .SC_BOOT_MODE != BootModeEnum .PRODUCTION :
0 commit comments