File size: 2,044 Bytes
9887034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os
import httpx
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse

app = FastAPI(title="Continuity Code Camera Relay")
LOCAL_ENGINE_URL = os.getenv("LOCAL_ENGINE_URL", "").rstrip("/")

@app.get("/", response_class=HTMLResponse)
def index():
    target = LOCAL_ENGINE_URL or "SET_LOCAL_ENGINE_URL_SECRET"
    return f"""<!doctype html>
<html><head><meta name="viewport" content="width=device-width,initial-scale=1"><title>Continuity Code Camera Relay</title></head>
<body style="font-family:system-ui;background:#070910;color:#fff;padding:20px">
<h1>Continuity Code Camera Relay</h1>
<p>This Space relays to your local Ollama-backed live engine.</p>
<p><b>Target:</b> {target}</p>
<p>Open the local/tunnel target directly for camera permissions and lowest latency:</p>
<p><a style="color:#82aaff" href="{target}">{target}</a></p>
</body></html>"""

@app.get("/health")
async def health():
    if not LOCAL_ENGINE_URL:
        return {"ok": False, "reason": "LOCAL_ENGINE_URL not set"}
    async with httpx.AsyncClient(timeout=10) as client:
        r = await client.get(f"{LOCAL_ENGINE_URL}/health")
        return {"ok": True, "relay_target": LOCAL_ENGINE_URL, "engine": r.json()}

@app.api_route("/proxy/{path:path}", methods=["GET","POST","PUT","DELETE","PATCH"])
async def proxy(path: str, request: Request):
    if not LOCAL_ENGINE_URL:
        return JSONResponse({"ok": False, "reason": "LOCAL_ENGINE_URL not set"}, status_code=500)
    body = await request.body()
    headers = {k:v for k,v in request.headers.items() if k.lower() not in {"host","content-length"}}
    async with httpx.AsyncClient(timeout=60) as client:
        r = await client.request(request.method, f"{LOCAL_ENGINE_URL}/{path}", content=body, headers=headers, params=request.query_params)
        ct = r.headers.get("content-type","")
        if "application/json" in ct:
            return JSONResponse(r.json(), status_code=r.status_code)
        return JSONResponse({"text": r.text}, status_code=r.status_code)