fastapi_middleware_toolkit.lifespan

Lifespan context manager factory for FastAPI applications.

Extracted from GridFlow backend/src/infrastructure/app_factory.py lines 18-26

Attributes

Functions

create_lifespan_manager(→ Callable)

Create a lifespan factory for FastAPI application.

create_health_check_endpoint(→ Callable)

Create a health check endpoint function.

Module Contents

fastapi_middleware_toolkit.lifespan.logger
fastapi_middleware_toolkit.lifespan.create_lifespan_manager(on_startup: Callable | None = None, on_shutdown: Callable | None = None, service_name: str = 'api', service_version: str = '1.0.0') Callable

Create a lifespan factory for FastAPI application.

Returns a callable compatible with FastAPI’s lifespan parameter.

Args:

on_startup: Optional async function to call on startup on_shutdown: Optional async function to call on shutdown service_name: Name of the service for logging service_version: Version of the service for logging

Returns:

Lifespan callable for FastAPI(lifespan=…)

Example:
>>> lifespan = create_lifespan_manager(
...     on_startup=init_database,
...     service_name="my-api"
... )
>>> app = FastAPI(lifespan=lifespan)
fastapi_middleware_toolkit.lifespan.create_health_check_endpoint(service_name: str, version: str = '1.0.0', additional_checks: Dict[str, Any] | None = None) Callable

Create a health check endpoint function.

Args:

service_name: Name of the service version: Version of the service additional_checks: Optional dict with additional health check data

Returns:

Async function that can be used as a FastAPI endpoint

Example:
>>> app = FastAPI()
>>> health_check = create_health_check_endpoint("my-api", "1.0.0")
>>> app.get("/health")(health_check)