Skip to content

Commit 010b798

Browse files
committed
fix: replace deprecated @app.on_event with lifespan handlers
Replace deprecated @app.on_event("startup") and @app.on_event("shutdown") with the modern lifespan context manager pattern using asynccontextmanager. This removes deprecation warnings from test output and uses the recommended FastAPI approach for application lifecycle events.
1 parent dd54a5e commit 010b798

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

backend-py/src/main.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
import uuid
3+
from contextlib import asynccontextmanager
34

45
import structlog
56
from fastapi import FastAPI, Request
@@ -25,10 +26,27 @@
2526
},
2627
]
2728

29+
30+
@asynccontextmanager
31+
async def lifespan(app: FastAPI):
32+
"""Lifespan context manager for startup and shutdown events."""
33+
# Startup
34+
logger.info(
35+
"Application startup complete",
36+
service="backend-py",
37+
version="0.1.0",
38+
api_prefix=api_prefix_v1,
39+
)
40+
yield
41+
# Shutdown
42+
logger.info("Application shutdown initiated")
43+
44+
2845
app = FastAPI(
2946
title=OpenAPIInfo["title"],
3047
version=OpenAPIInfo["version"],
3148
openapi_tags=tags_metadata,
49+
lifespan=lifespan,
3250
)
3351

3452

@@ -147,20 +165,3 @@ async def root():
147165

148166

149167
app.include_router(user_router, prefix=api_prefix_v1 + "/user", tags=["User CRUD"])
150-
151-
152-
# Startup event
153-
@app.on_event("startup")
154-
async def startup_event():
155-
logger.info(
156-
"Application startup complete",
157-
service="backend-py",
158-
version="0.1.0",
159-
api_prefix=api_prefix_v1,
160-
)
161-
162-
163-
# Shutdown event
164-
@app.on_event("shutdown")
165-
async def shutdown_event():
166-
logger.info("Application shutdown initiated")

0 commit comments

Comments
 (0)