"""Explicit custom routes for Gradio Server mode. Node owns application paths.""" import httpx from fastapi import Request from starlette.responses import JSONResponse, Response def attach_studio(server, port): async def forward(request: Request): try: headers = {k: v for k, v in request.headers.items() if k not in {"connection", "transfer-encoding", "accept-encoding"}} url = f"http://127.0.0.1:{port}{request.url.path}" if request.url.query: url += "?" + request.url.query async with httpx.AsyncClient(timeout=25, trust_env=False) as client: upstream = await client.request(request.method, url, content=await request.body(), headers=headers) response = Response(content=upstream.content, status_code=upstream.status_code) for k, value in upstream.headers.multi_items(): if k.lower() not in {"transfer-encoding", "content-encoding", "content-length", "connection", "date", "server", "keep-alive"}: response.headers.append(k, value) return response except httpx.RequestError: return JSONResponse({"detail":"Studio belum siap. Coba lagi sebentar."}, status_code=503) for path in ["/", "/index.html", "/health", "/images/{path:path}", "/assets/{path:path}", "/models/{path:path}", "/data/{path:path}", "/benchmark/{path:path}", "/logos/{path:path}", "/licenses/{path:path}"]: server.add_api_route(path, forward, methods=["GET", "HEAD"], include_in_schema=False) server.add_api_route("/api/{path:path}", forward, methods=["POST", "GET", "HEAD"], include_in_schema=False)