File tree Expand file tree Collapse file tree 1 file changed +18
-17
lines changed
Expand file tree Collapse file tree 1 file changed +18
-17
lines changed Original file line number Diff line number Diff line change 11import time
22import uuid
3+ from contextlib import asynccontextmanager
34
45import structlog
56from fastapi import FastAPI , Request
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+
2845app = 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
162180app .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" )
You can’t perform that action at this time.
0 commit comments