Skip to content

Commit 7fdfc6b

Browse files
authored
fix: replace deprecated @app.on_event with lifespan handlers (#419)
1 parent 7a5f3c4 commit 7fdfc6b

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

@@ -160,20 +178,3 @@ async def root():
160178

161179

162180
app.include_router(user_router, prefix=api_prefix_v1 + "/user", tags=["User CRUD"])
163-
164-
165-
# Startup event
166-
@app.on_event("startup")
167-
async def startup_event():
168-
logger.info(
169-
"Application startup complete",
170-
service="backend-py",
171-
version="0.1.0",
172-
api_prefix=api_prefix_v1,
173-
)
174-
175-
176-
# Shutdown event
177-
@app.on_event("shutdown")
178-
async def shutdown_event():
179-
logger.info("Application shutdown initiated")

0 commit comments

Comments
 (0)