Spaces:
Runtime error
Runtime error
Commit
·
b06b4e5
1
Parent(s):
6be4de2
Add MongoDb
Browse files- app/main.py +11 -11
app/main.py
CHANGED
|
@@ -39,18 +39,18 @@ if not mongodb_uri or not db_name:
|
|
| 39 |
logger.error("Environment variables mongodb_uri or DB_NAME are not set.")
|
| 40 |
raise ValueError("Environment variables mongodb_uri or DB_NAME are not set.")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
app = FastAPI()
|
| 56 |
app.add_middleware(
|
|
|
|
| 39 |
logger.error("Environment variables mongodb_uri or DB_NAME are not set.")
|
| 40 |
raise ValueError("Environment variables mongodb_uri or DB_NAME are not set.")
|
| 41 |
|
| 42 |
+
@asynccontextmanager
|
| 43 |
+
async def lifespan(app: FastAPI):
|
| 44 |
+
app.mongodb_client = MongoClient(mongodb_uri)
|
| 45 |
+
app.database = app.mongodb_client[db_name]
|
| 46 |
+
logger.info("Connected to the MongoDB database!")
|
| 47 |
|
| 48 |
+
try:
|
| 49 |
+
collections = app.database.list_collection_names()
|
| 50 |
+
print(f"Collections in {db_name}: {collections}")
|
| 51 |
+
yield
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.error(e)
|
| 54 |
|
| 55 |
app = FastAPI()
|
| 56 |
app.add_middleware(
|