File size: 1,495 Bytes
c8db0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse, HTMLResponse
import httpx
import asyncio

app = FastAPI()

@app.get("/")
def greet_json():
    return {"Hello": "World!", "code_server": "Available at /code"}

@app.get("/code")
async def redirect_to_code():
    """Redirect to code-server"""
    return RedirectResponse(url="/code/")

@app.get("/code/{path:path}")
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
            )

@app.get("/health")
def health_check():
    return {"status": "healthy", "services": ["fastapi", "code-server"]}