-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
27 lines (24 loc) · 792 Bytes
/
db.py
File metadata and controls
27 lines (24 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from pymongo import MongoClient
from pymongo.errors import PyMongoError
import os
from dotenv import load_dotenv
load_dotenv()
# "mongodb://localhost:27017"
MONGO_URI = os.getenv("MONGO_URI")
client = None
db = None
analysis_collection = None
mongo_available = False
mongo_error = None
try:
# Keep startup resilient: if MongoDB is down, the app should still boot and skip caching.
client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=3000)
client.admin.command("ping")
db = client["code-ripple"]
analysis_collection = db["analysis"]
analysis_collection.create_index("cache_key", unique=True)
mongo_available = True
print("Connected to MongoDB successfully.")
except PyMongoError as exc:
mongo_error = str(exc)
print("Mongo ERROR:", mongo_error)