51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
from fastapi import APIRouter, Request
|
|
from fastapi.responses import HTMLResponse, Response
|
|
|
|
router = APIRouter()
|
|
|
|
_API_URL = os.getenv("API_URL", "http://api:8000")
|
|
_DIST = Path(__file__).parent / "editor-ui" / "dist"
|
|
|
|
_SKIP_REQ = {"host", "content-length", "transfer-encoding"}
|
|
_SKIP_RES = {"content-encoding", "transfer-encoding", "content-length"}
|
|
|
|
|
|
# ── API proxy: browser → Flet server → internal API ──────────────────────────
|
|
|
|
@router.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
|
async def api_proxy(path: str, request: Request):
|
|
headers = {k: v for k, v in request.headers.items() if k.lower() not in _SKIP_REQ}
|
|
body = await request.body()
|
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
|
r = await client.request(
|
|
method=request.method,
|
|
url=f"{_API_URL}/{path}",
|
|
headers=headers,
|
|
content=body,
|
|
params=dict(request.query_params),
|
|
)
|
|
res_headers = {k: v for k, v in r.headers.items() if k.lower() not in _SKIP_RES}
|
|
return Response(
|
|
content=r.content,
|
|
status_code=r.status_code,
|
|
headers=res_headers,
|
|
media_type=r.headers.get("content-type"),
|
|
)
|
|
|
|
|
|
# ── React editor SPA ──────────────────────────────────────────────────────────
|
|
|
|
@router.get("/editor/{article_id}")
|
|
async def editor_page(article_id: str):
|
|
index = _DIST / "index.html"
|
|
if not index.exists():
|
|
return HTMLResponse(
|
|
"<h2>Editor not built.</h2><p>Run <code>docker-compose up --build</code></p>",
|
|
status_code=503,
|
|
)
|
|
return HTMLResponse(index.read_text(encoding="utf-8"))
|