diff --git a/unwind/web.py b/unwind/web.py index cab9a9e..eb08e9c 100644 --- a/unwind/web.py +++ b/unwind/web.py @@ -1,4 +1,5 @@ import asyncio +import contextlib import logging import secrets from json.decoder import JSONDecodeError @@ -612,6 +613,13 @@ def auth_error(request, err): return unauthorized(str(err)) +@contextlib.asynccontextmanager +async def lifespan(app: Starlette): + await open_connection_pool() + yield + await close_connection_pool() + + def create_app(): if config.loglevel == "DEBUG": logging.basicConfig( @@ -622,8 +630,7 @@ def create_app(): log.debug(f"Log level: {config.loglevel}") return Starlette( - on_startup=[open_connection_pool], - on_shutdown=[close_connection_pool], + lifespan=lifespan, routes=[ Mount(f"{config.api_base}v1", routes=route.registered), ],