Spaces:
Sleeping
Sleeping
editted database.py
Browse files- backend/database.py +19 -5
backend/database.py
CHANGED
|
@@ -14,15 +14,29 @@ load_dotenv()
|
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
logging.info('Environment variables loaded successfully.')
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# MongoDB configuration
|
| 18 |
-
MONGODB_URI =
|
| 19 |
-
DB_NAME =
|
| 20 |
|
| 21 |
# Cloudinary Configuration
|
| 22 |
cloudinary.config(
|
| 23 |
-
cloud_name=
|
| 24 |
-
api_key=
|
| 25 |
-
api_secret=
|
| 26 |
secure=True
|
| 27 |
)
|
| 28 |
|
|
|
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
logging.info('Environment variables loaded successfully.')
|
| 16 |
|
| 17 |
+
|
| 18 |
+
def _get_env_var(*names: str) -> str:
|
| 19 |
+
"""
|
| 20 |
+
Returns the first configured environment variable from a list of candidates.
|
| 21 |
+
|
| 22 |
+
Hugging Face Spaces exposes secrets as environment variables, so this helper
|
| 23 |
+
accepts both the original project names and common aliases.
|
| 24 |
+
"""
|
| 25 |
+
for name in names:
|
| 26 |
+
value = os.getenv(name)
|
| 27 |
+
if value:
|
| 28 |
+
return value
|
| 29 |
+
raise RuntimeError(f"Missing required environment variable. Tried: {', '.join(names)}")
|
| 30 |
+
|
| 31 |
# MongoDB configuration
|
| 32 |
+
MONGODB_URI = _get_env_var('MONGODB_URI', 'MONGO_URI')
|
| 33 |
+
DB_NAME = _get_env_var('DATABASE_NAME', 'DB_NAME')
|
| 34 |
|
| 35 |
# Cloudinary Configuration
|
| 36 |
cloudinary.config(
|
| 37 |
+
cloud_name=_get_env_var('CLOUDINARY_CLOUD_NAME'),
|
| 38 |
+
api_key=_get_env_var('CLOUDINARY_API_KEY'),
|
| 39 |
+
api_secret=_get_env_var('CLOUDINARY_API_SECRET'),
|
| 40 |
secure=True
|
| 41 |
)
|
| 42 |
|