Add fallback for MONGO_DB_NAME from MONGO_URI

Co-authored-by: mathieumoran1 <[email protected]>
This commit is contained in:
Cursor Agent
2025-12-22 06:00:31 +00:00
co-authored by mathieumoran1
parent 60fd5a25ce
commit 53eb7faf06
2 changed files with 29 additions and 1 deletions
+29 -1
View File
@@ -5,6 +5,7 @@ from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Generator, Optional, Tuple from typing import Generator, Optional, Tuple
from urllib.parse import quote from urllib.parse import quote
from urllib.parse import urlparse
import jwt import jwt
from bson import ObjectId from bson import ObjectId
@@ -28,6 +29,26 @@ def _mongo_uri() -> str:
# Read env at runtime (important on Windows/uvicorn worker reloads). # Read env at runtime (important on Windows/uvicorn worker reloads).
return _env_str("MONGO_URI") return _env_str("MONGO_URI")
def _mongo_db_name() -> str:
# Prefer explicit env var, otherwise try to parse from URI path.
explicit = _env_str("MONGO_DB_NAME")
if explicit:
return explicit
uri = _mongo_uri()
if not uri:
return ""
try:
parsed = urlparse(uri)
# Path is like "/dbname"
db = (parsed.path or "").lstrip("/")
# Strip any extra segments just in case
db = db.split("/")[0].strip()
return db
except Exception:
return ""
def _video_files_dir() -> Path: def _video_files_dir() -> Path:
# Read env at runtime (important on Windows/uvicorn worker reloads). # Read env at runtime (important on Windows/uvicorn worker reloads).
@@ -191,7 +212,13 @@ def stream_video(
raise HTTPException(status_code=400, detail="Invalid movie id") raise HTTPException(status_code=400, detail="Invalid movie id")
with _mongo_client() as client: with _mongo_client() as client:
db = client.get_default_database() db_name = _mongo_db_name()
if not db_name:
raise HTTPException(
status_code=500,
detail="Mongo DB name not configured. Add it to MONGO_URI (…/dbname) or set MONGO_DB_NAME.",
)
db = client[db_name]
movie = db["movies"].find_one({"_id": oid}, {"video_file": 1}) movie = db["movies"].find_one({"_id": oid}, {"video_file": 1})
video_file = (movie or {}).get("video_file") or "" video_file = (movie or {}).get("video_file") or ""
@@ -259,5 +286,6 @@ def healthz():
"ok": True, "ok": True,
"time": datetime.now(timezone.utc).isoformat(), "time": datetime.now(timezone.utc).isoformat(),
"video_files_dir": str(_video_files_dir()), "video_files_dir": str(_video_files_dir()),
"mongo_db_name": _mongo_db_name() or None,
} }