[Issue #1312]: full examples use lifespan instead of on_startup (#1363)

* docs(examples): full examples use lifespan instead of on_startup for database initialization

* Update beanie.md

Replaced the annotations on the notes that I missed
This commit is contained in:
Brandon H. Goding
2024-03-05 03:09:49 -05:00
committed by GitHub
parent 0df82afb32
commit 2ffb7006ff
5 changed files with 60 additions and 39 deletions

View File

@@ -1,10 +1,20 @@
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI
from app.db import User, create_db_and_tables
from app.schemas import UserCreate, UserRead, UserUpdate
from app.users import auth_backend, current_active_user, fastapi_users
app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
# Not needed if you setup a migration system like Alembic
await create_db_and_tables()
yield
app = FastAPI(lifespan=lifespan)
app.include_router(
fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"]
@@ -34,9 +44,3 @@ app.include_router(
@app.get("/authenticated-route")
async def authenticated_route(user: User = Depends(current_active_user)):
return {"message": f"Hello {user.email}!"}
@app.on_event("startup")
async def on_startup():
# Not needed if you setup a migration system like Alembic
await create_db_and_tables()