Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/api/docling.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,27 @@ def determine_docling_host() -> str:
return "localhost"


# Use explicit URL if provided, otherwise auto-detect host
_docling_url_override = os.getenv("DOCLING_SERVE_URL")
if _docling_url_override:
DOCLING_SERVICE_URL = _docling_url_override.rstrip("/")
HOST_IP = _docling_url_override # For display in health responses
logger.info("Using DOCLING_SERVE_URL override: %s", DOCLING_SERVICE_URL)
else:
HOST_IP = determine_docling_host()
DOCLING_SERVICE_URL = f"http://{HOST_IP}:5001"
def get_docling_service_url() -> str:
"""Return the Docling Serve URL, reading DOCLING_SERVE_URL from the environment
at call time so that runtime changes to the variable are picked up without
requiring a server restart.
"""
override = os.getenv("DOCLING_SERVE_URL")
if override:
url = override.rstrip("/")
logger.debug("Using DOCLING_SERVE_URL override: %s", url)
return url
host = determine_docling_host()
return f"http://{host}:5001"


async def health(request: Request) -> JSONResponse:
"""
Proxy health check to docling-serve.
This allows the frontend to check docling status via same-origin request.
"""
health_url = f"{DOCLING_SERVICE_URL}/health"
docling_url = get_docling_service_url()
health_url = f"{docling_url}/health"
try:
async with httpx.AsyncClient() as client:
response = await client.get(
Expand All @@ -104,27 +108,27 @@ async def health(request: Request) -> JSONResponse:
if response.status_code == 200:
return JSONResponse({
"status": "healthy",
"host": HOST_IP
"host": docling_url
})
else:
logger.warning("Docling health check failed", url=health_url, status_code=response.status_code)
return JSONResponse({
"status": "unhealthy",
"message": f"Health check failed with status: {response.status_code}",
"host": HOST_IP
"host": docling_url
}, status_code=503)

except httpx.TimeoutException:
logger.warning("Docling health check timeout", url=health_url)
return JSONResponse({
"status": "unhealthy",
"message": "Connection timeout",
"host": HOST_IP
"host": docling_url
}, status_code=503)
except Exception as e:
logger.error("Docling health check failed", url=health_url, error=str(e))
return JSONResponse({
"status": "unhealthy",
"message": str(e),
"host": HOST_IP
"host": docling_url
}, status_code=503)
7 changes: 4 additions & 3 deletions src/utils/docling_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ async def _send_convert_request(
file_bytes: bytes,
) -> dict:
"""Send a file to docling-serve and return the DoclingDocument dict."""
from api.docling import DOCLING_SERVICE_URL
from api.docling import get_docling_service_url

url = f"{DOCLING_SERVICE_URL}/v1/convert/file"
docling_url = get_docling_service_url()
url = f"{docling_url}/v1/convert/file"

ocr_engine = os.getenv("DOCLING_OCR_ENGINE")
data: dict[str, str] = {"to_formats": "json"}
Expand All @@ -41,7 +42,7 @@ async def _send_convert_request(
response.raise_for_status()
except httpx.ConnectError as exc:
raise DoclingServeError(
f"Cannot connect to docling-serve at {DOCLING_SERVICE_URL}. "
f"Cannot connect to docling-serve at {docling_url}. "
f"Is it running? Start with: uvx docling-serve run"
) from exc
except httpx.TimeoutException as exc:
Expand Down
Loading