From baf97bd159f8308a76b154a7e17851ad016376e4 Mon Sep 17 00:00:00 2001 From: moson Date: Fri, 1 Dec 2023 20:14:06 +0100 Subject: [PATCH] fix(test): FastAPI 0.104.1 - Fix warnings FastAPI events are deprecated. Use "Lifespan" function instead. Signed-off-by: moson --- aurweb/asgi.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/aurweb/asgi.py b/aurweb/asgi.py index 9b6ffcb3..dfe2d32d 100644 --- a/aurweb/asgi.py +++ b/aurweb/asgi.py @@ -6,6 +6,7 @@ import re import sys import traceback import typing +from contextlib import asynccontextmanager from urllib.parse import quote_plus import requests @@ -33,11 +34,18 @@ from aurweb.routers import APP_ROUTES from aurweb.templates import make_context, render_template logger = aur_logging.get_logger(__name__) +session_secret = aurweb.config.get("fastapi", "session_secret") + + +@asynccontextmanager +async def lifespan(app: FastAPI): + await app_startup() + yield + # Setup the FastAPI app. -app = FastAPI() +app = FastAPI(lifespan=lifespan) -session_secret = aurweb.config.get("fastapi", "session_secret") # Instrument routes with the prometheus-fastapi-instrumentator # library with custom collectors and expose /metrics. @@ -46,7 +54,6 @@ instrumentator().add(prometheus.http_requests_total()) instrumentator().instrument(app) -@app.on_event("startup") async def app_startup(): # https://stackoverflow.com/questions/67054759/about-the-maximum-recursion-error-in-fastapi # Test failures have been observed by internal starlette code when