From af4556c0c05b1553b700f2df835c48df9b76a96a Mon Sep 17 00:00:00 2001 From: ronan-boue <66888455+ronan-boue@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:09:27 -0500 Subject: [PATCH] Update main.py Included lifespan to correct startup and shutdown deprecation --- main.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 7879ac6..c939efa 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from fastapi import FastAPI from dotenv import dotenv_values from pymongo import MongoClient @@ -5,16 +6,13 @@ config = dotenv_values(".env") -app = FastAPI() - -@app.on_event("startup") -def startup_db_client(): +@asynccontextmanager +async def lifespan(app: FastAPI): app.mongodb_client = MongoClient(config["ATLAS_URI"]) app.database = app.mongodb_client[config["DB_NAME"]] - -@app.on_event("shutdown") -def shutdown_db_client(): + yield app.mongodb_client.close() -app.include_router(book_router, tags=["books"], prefix="/book") +app = FastAPI(lifespan=lifespan) +app.include_router(book_router, tags=["books"], prefix="/book")