Closing / database /connection.py
madhurithika22
deploy: add backend codebase and Dockerfile configuration for Hugging Face Spaces
35512c1
Raw
History Blame Contribute Delete
1.16 kB
from motor.motor_asyncio import AsyncIOMotorClient
from core.config import settings
class DatabaseClient:
client: AsyncIOMotorClient = None
def connect(self):
if getattr(settings, "STATELESS_MODE", False) or not settings.MONGO_URI or settings.MONGO_URI.lower() in ("none", "null", ""):
print("[STATELESS MODE ACTIVE] Bypassing MongoDB client initialization.")
self.client = None
return
try:
# We configure a short connection timeout so that it fails fast and transparently
# falls back to the file system if MongoDB is not available on localhost.
self.client = AsyncIOMotorClient(settings.MONGO_URI, serverSelectionTimeoutMS=2000, tlsAllowInvalidCertificates=True)
print("MongoDB client initialized successfully.")
except Exception as e:
print(f"MongoDB initialization error: {e}")
self.client = None
def disconnect(self):
if self.client:
try:
self.client.close()
except Exception:
pass
self.client = None
db_client = DatabaseClient()