Spaces:
Sleeping
Sleeping
Update src/agri_predict/config.py
Browse files
src/agri_predict/config.py
CHANGED
|
@@ -9,7 +9,6 @@ load_dotenv()
|
|
| 9 |
|
| 10 |
def get_collections():
|
| 11 |
"""Return a dict with MongoDB collections used by the app.
|
| 12 |
-
|
| 13 |
Loads `MONGO_URI` from environment and connects with TLS CA validation.
|
| 14 |
Raises RuntimeError if `MONGO_URI` is missing.
|
| 15 |
"""
|
|
@@ -17,7 +16,14 @@ def get_collections():
|
|
| 17 |
if not mongo_uri:
|
| 18 |
raise RuntimeError("MONGO_URI is not set in the environment")
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
db = client["AgriPredict"]
|
| 22 |
|
| 23 |
# Create indexes for better query performance (safe to call multiple times)
|
|
|
|
| 9 |
|
| 10 |
def get_collections():
|
| 11 |
"""Return a dict with MongoDB collections used by the app.
|
|
|
|
| 12 |
Loads `MONGO_URI` from environment and connects with TLS CA validation.
|
| 13 |
Raises RuntimeError if `MONGO_URI` is missing.
|
| 14 |
"""
|
|
|
|
| 16 |
if not mongo_uri:
|
| 17 |
raise RuntimeError("MONGO_URI is not set in the environment")
|
| 18 |
|
| 19 |
+
# Check if it's a cloud cluster (mongodb+srv or contains mongodb.net)
|
| 20 |
+
# Use SSL for cloud clusters, skip SSL for self-hosted instances
|
| 21 |
+
if "mongodb+srv" in mongo_uri or "mongodb.net" in mongo_uri:
|
| 22 |
+
client = MongoClient(mongo_uri, tlsCAFile=certifi.where())
|
| 23 |
+
else:
|
| 24 |
+
# Self-hosted MongoDB - connect without SSL
|
| 25 |
+
client = MongoClient(mongo_uri)
|
| 26 |
+
|
| 27 |
db = client["AgriPredict"]
|
| 28 |
|
| 29 |
# Create indexes for better query performance (safe to call multiple times)
|