Spaces:
Paused
Paused
| from fastapi import FastAPI, Request | |
| from fastapi.responses import RedirectResponse, HTMLResponse | |
| import httpx | |
| import asyncio | |
| app = FastAPI() | |
| def greet_json(): | |
| return {"Hello": "World!", "code_server": "Available at /code"} | |
| async def redirect_to_code(): | |
| """Redirect to code-server""" | |
| return RedirectResponse(url="/code/") | |
| async def proxy_code_server(request: Request, path: str = ""): | |
| """Proxy requests to code-server running on port 7860""" | |
| target_url = f"http://localhost:7860/{path}" | |
| async with httpx.AsyncClient() as client: | |
| try: | |
| # Forward the request to code-server | |
| response = await client.request( | |
| method=request.method, | |
| url=target_url, | |
| headers=dict(request.headers), | |
| params=request.query_params, | |
| timeout=30.0 | |
| ) | |
| # Return the response from code-server | |
| return HTMLResponse( | |
| content=response.content, | |
| status_code=response.status_code, | |
| headers=dict(response.headers) | |
| ) | |
| except Exception as e: | |
| return HTMLResponse( | |
| content=f"<h1>Code Server Not Available</h1><p>Error: {str(e)}</p>", | |
| status_code=503 | |
| ) | |
| def health_check(): | |
| return {"status": "healthy", "services": ["fastapi", "code-server"]} |