SPB Git

spb/llm-api

Public
2.5 KB · 51 lines python
1"""Reverse-proxy everything that is not API to the Next.js dashboard (127.0.0.1:8301)."""23from __future__ import annotations45import httpx6from fastapi import APIRouter, Request, Response7from fastapi.responses import HTMLResponse, StreamingResponse89router = APIRouter()1011HOP_BY_HOP = {"connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers",12              "transfer-encoding", "upgrade", "content-length", "content-encoding"}1314FALLBACK_HTML = """<!doctype html><html><head><meta charset="utf-8"><title>LLM API</title>15<style>body{font-family:-apple-system,system-ui,sans-serif;background:#0b0d10;color:#e6e8eb;display:grid;place-items:center;height:100vh;margin:0}16main{max-width:520px;padding:32px;border:1px solid #23272d;border-radius:14px;background:#11141a}code{color:#8fd3ff}</style></head>17<body><main><h1>LLM API</h1><p>The API is running but the dashboard is not reachable yet.</p>18<p>OpenAI-compatible endpoint: <code>/v1/chat/completions</code> · health: <code>/health</code></p></main></body></html>"""192021@router.api_route("/{path:path}", methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], include_in_schema=False)22async def proxy(path: str, request: Request):23    settings = request.app.state.settings24    client: httpx.AsyncClient = request.app.state.ui_client25    url = f"{settings.web_url.rstrip('/')}/{path}"26    if request.url.query:27        url += f"?{request.url.query}"28    headers = {k: v for k, v in request.headers.items() if k.lower() not in HOP_BY_HOP and k.lower() != "host"}29    headers["x-forwarded-host"] = request.headers.get("host", "")30    headers["x-forwarded-proto"] = request.headers.get("x-forwarded-proto", request.url.scheme)31    body = await request.body()32    try:33        req = client.build_request(request.method, url, headers=headers, content=body)34        resp = await client.send(req, stream=True)35    except httpx.HTTPError:36        return HTMLResponse(FALLBACK_HTML, status_code=503)37    out_headers = {k: v for k, v in resp.headers.items() if k.lower() not in HOP_BY_HOP}3839    async def body_iter():40        try:41            async for chunk in resp.aiter_raw():42                yield chunk43        finally:44            await resp.aclose()4546    if request.method == "HEAD":47        await resp.aclose()48        return Response(status_code=resp.status_code, headers=out_headers)49    return StreamingResponse(body_iter(), status_code=resp.status_code, headers=out_headers,50                             media_type=resp.headers.get("content-type"))51
Files · 790eb7a2c85f25590cd373b6499a6befcbb82feb
Menu

Theme