Skip to content

Commit 7a922d2

Browse files
feat: Add lifespan support to FastAPI app creation
Add optional lifespan parameter to create_fastapi_app function to support setup and teardown operations in the FastAPI application lifecycle. This enables proper resource management and initialization when creating FastAPI applications.
1 parent fd7306f commit 7a922d2

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

archipy/helpers/utils/app_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from collections.abc import Awaitable, Callable
55
from http import HTTPStatus
6-
from typing import Any, cast
6+
from typing import Any, cast, AsyncContextManager
77

88
from fastapi import FastAPI, Request, Response
99
from fastapi.exceptions import RequestValidationError
@@ -229,12 +229,14 @@ def create_fastapi_app(
229229
config: BaseConfig | None = None,
230230
*,
231231
configure_exception_handlers: bool = True,
232+
lifespan: Callable[..., AsyncContextManager] | None = None,
232233
) -> FastAPI:
233234
"""Creates and configures a FastAPI application.
234235
235236
Args:
236237
config (BaseConfig | None): Optional custom configuration. If not provided, uses global config.
237238
configure_exception_handlers (bool): Whether to configure exception handlers.
239+
lifespan (Callable[..., AsyncContextManager] | None): Optional lifespan context manager for the app.
238240
239241
Returns:
240242
FastAPI: The configured FastAPI application instance.
@@ -253,6 +255,7 @@ def create_fastapi_app(
253255
docs_url=config.FASTAPI.DOCS_URL,
254256
redocs_url=config.FASTAPI.RE_DOCS_URL,
255257
responses=cast(dict[int | str, Any], common_responses),
258+
lifespan=lifespan,
256259
)
257260

258261
FastAPIUtils.setup_sentry(config)

0 commit comments

Comments
 (0)