Spaces:
Runtime error
Runtime error
Download database/connection.py from madhurithika22/Moulding: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/madhurithika22/Moulding/resolve/main/database/connection.py
- Command line
-
hf download hf://spaces/madhurithika22/Moulding/database/connection.py
-
curl -L -o connection.py https://huggingface.co/spaces/madhurithika22/Moulding/resolve/main/database/connection.py
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() |