Skip to content

Commit f249dbd

Browse files
committed
don't use depreciated method on_event
1 parent 133dba4 commit f249dbd

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

app.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
from routes.users import UserRouter
33
from fastapi import FastAPI
44
from database.database import engine
5+
from contextlib import asynccontextmanager
56

6-
7-
app = FastAPI(description="Template for building FastAPI applications with PostgreSQL", contact={"email": "samanidarix@gmail.com"})
8-
9-
10-
11-
12-
@app.on_event("startup")
13-
async def startup():
7+
@asynccontextmanager
8+
async def lifespan(app: FastAPI):
149
await engine.connect()
10+
yield
11+
# You can also close DB connections here if needed
12+
print("App shutdown complete.")
13+
await engine.dispose()
14+
1515

16-
@app.on_event("shutdown")
17-
async def shutdown():
18-
await engine.dispose()
16+
app = FastAPI(lifespan=lifespan, description="Template for building FastAPI applications with PostgreSQL", contact={"email": "samanidarix@gmail.com"})
1917

2018

2119

0 commit comments

Comments
 (0)