userIdc2024 commited on
Commit
2876716
·
verified ·
1 Parent(s): 40e96bd

Update database/connections.py

Browse files
Files changed (1) hide show
  1. database/connections.py +17 -8
database/connections.py CHANGED
@@ -6,14 +6,23 @@ import streamlit as st
6
 
7
  @st.cache_resource
8
  def get_mongo_client():
9
-
10
- uri = cnf.MONGO_URI
11
- dbname = cnf.MONGO_DB
12
- client = MongoClient(uri)
13
- db = client[dbname]
14
- db.command("ping")
15
- logger.info("MongoDB connection established successfully")
16
- return db
 
 
 
 
 
 
 
 
 
17
 
18
  def get_results_collection():
19
  db = get_mongo_client()
 
6
 
7
  @st.cache_resource
8
  def get_mongo_client():
9
+ try:
10
+ uri = cnf.MONGO_URI
11
+ dbname = cnf.MONGO_DB
12
+
13
+ # Check if required environment variables are set
14
+ if not uri or not dbname:
15
+ logger.warning("MongoDB configuration not found. Running without database.")
16
+ return None
17
+
18
+ client = MongoClient(uri)
19
+ db = client[dbname]
20
+ db.command("ping")
21
+ logger.info("MongoDB connection established successfully")
22
+ return db
23
+ except Exception as e:
24
+ logger.error(f"MongoDB connection failed: {e}")
25
+ return None
26
 
27
  def get_results_collection():
28
  db = get_mongo_client()