-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (36 loc) · 1.54 KB
/
Copy pathmain.py
File metadata and controls
50 lines (36 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from document_converter.route import router as document_converter_router
logging.basicConfig(level=logging.INFO)
app = FastAPI(
title="Document to Markdown API",
description="Convert PDF, DOCX, PPTX, HTML, images, AsciiDoc, Markdown, and CSV to Markdown.",
version="0.1.0",
)
# Browsers reject `Access-Control-Allow-Origin: *` when credentials are included,
# so keep the two consistent. Origins can be overridden via CORS_ALLOW_ORIGINS.
_cors_origins_env = os.getenv("CORS_ALLOW_ORIGINS", "*")
_cors_origins = [origin.strip() for origin in _cors_origins_env.split(",") if origin.strip()]
_allow_credentials = _cors_origins != ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=_cors_origins,
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=_allow_credentials,
)
@app.get("/health", tags=["health"], summary="Liveness probe")
def health():
return {"status": "ok"}
@app.get("/health/ready", tags=["health"], summary="Readiness probe")
def health_ready():
from worker.celery_config import celery_app
try:
with celery_app.connection_for_read() as conn:
conn.ensure_connection(max_retries=1, timeout=2)
return {"status": "ok", "broker": "ok"}
except Exception as exc: # pragma: no cover - depends on runtime infra
return {"status": "degraded", "broker": f"unreachable: {exc}"}
app.include_router(document_converter_router, prefix="", tags=["document-converter"])