likhonsheikh commited on
Commit
c8db0bb
·
verified ·
1 Parent(s): 7261119

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import RedirectResponse, HTMLResponse
3
+ import httpx
4
+ import asyncio
5
+
6
+ app = FastAPI()
7
+
8
+ @app.get("/")
9
+ def greet_json():
10
+ return {"Hello": "World!", "code_server": "Available at /code"}
11
+
12
+ @app.get("/code")
13
+ async def redirect_to_code():
14
+ """Redirect to code-server"""
15
+ return RedirectResponse(url="/code/")
16
+
17
+ @app.get("/code/{path:path}")
18
+ async def proxy_code_server(request: Request, path: str = ""):
19
+ """Proxy requests to code-server running on port 7860"""
20
+ target_url = f"http://localhost:7860/{path}"
21
+
22
+ async with httpx.AsyncClient() as client:
23
+ try:
24
+ # Forward the request to code-server
25
+ response = await client.request(
26
+ method=request.method,
27
+ url=target_url,
28
+ headers=dict(request.headers),
29
+ params=request.query_params,
30
+ timeout=30.0
31
+ )
32
+
33
+ # Return the response from code-server
34
+ return HTMLResponse(
35
+ content=response.content,
36
+ status_code=response.status_code,
37
+ headers=dict(response.headers)
38
+ )
39
+ except Exception as e:
40
+ return HTMLResponse(
41
+ content=f"<h1>Code Server Not Available</h1><p>Error: {str(e)}</p>",
42
+ status_code=503
43
+ )
44
+
45
+ @app.get("/health")
46
+ def health_check():
47
+ return {"status": "healthy", "services": ["fastapi", "code-server"]}