3v324v23 commited on
Commit
ae5c972
·
1 Parent(s): 6032a52

Fix blank screen: Implement robust Nginx Proxy with WebSocket support

Browse files
Files changed (4) hide show
  1. Dockerfile +5 -0
  2. gateway.py +0 -59
  3. nginx.conf +66 -0
  4. start.sh +7 -7
Dockerfile CHANGED
@@ -5,8 +5,12 @@ WORKDIR /app
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
7
  bash \
 
8
  && rm -rf /var/lib/apt/lists/*
9
 
 
 
 
10
  COPY requirements.txt .
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
@@ -18,4 +22,5 @@ RUN chmod +x start.sh
18
  ENV PYTHONPATH=/app
19
  EXPOSE 7860
20
 
 
21
  CMD ["./start.sh"]
 
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
7
  bash \
8
+ nginx \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
+ # Copy Nginx configuration
12
+ COPY nginx.conf /etc/nginx/nginx.conf
13
+
14
  COPY requirements.txt .
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
 
22
  ENV PYTHONPATH=/app
23
  EXPOSE 7860
24
 
25
+ # We use start.sh to launch FastAPI, Streamlit, and Nginx
26
  CMD ["./start.sh"]
gateway.py DELETED
@@ -1,59 +0,0 @@
1
- import os
2
- import httpx
3
- from fastapi import FastAPI, Request, Response
4
- from fastapi.responses import StreamingResponse
5
- from server.app import app as env_app
6
-
7
- # The Gateway App
8
- app = FastAPI(title="PR Review Gateway")
9
-
10
- # 1. Mount Environment API Routes (Primary for OpenEnv Validator)
11
- # These will be available on the main port 7860
12
- app.include_router(env_app.router)
13
-
14
- # 2. Proxy for Streamlit Dashboard (Internal port 8501)
15
- STREAMLIT_URL = "http://localhost:8501"
16
- client = httpx.AsyncClient(base_url=STREAMLIT_URL)
17
-
18
- @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
19
- async def proxy_to_streamlit(request: Request, path: str):
20
- """
21
- Catch-all route to proxy everything else to the Streamlit dashboard on port 8501.
22
- """
23
- url = f"{STREAMLIT_URL}/{path}"
24
-
25
- # Forward the request headers and body
26
- headers = dict(request.headers)
27
- # Remove 'host' to avoid proxy confusion
28
- headers.pop("host", None)
29
-
30
- # Handle Streamlit's WebSocket and long-polling if needed
31
- # (Note: For full WebSocket support, a more complex proxy is usually needed,
32
- # but for standard Streamlit on HF, this basic forwarder often works for the UI)
33
-
34
- # Get request body if it exists
35
- body = await request.body()
36
-
37
- # Send request to Streamlit
38
- try:
39
- response = await client.request(
40
- method=request.method,
41
- url=url,
42
- headers=headers,
43
- params=request.query_params,
44
- content=body,
45
- timeout=30.0
46
- )
47
-
48
- # Return the response back to the user
49
- return Response(
50
- content=response.content,
51
- status_code=response.status_code,
52
- headers=dict(response.headers)
53
- )
54
- except Exception as e:
55
- return Response(content=f"Gateway Error: {e}", status_code=502)
56
-
57
- if __name__ == "__main__":
58
- import uvicorn
59
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
nginx.conf ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ worker_processes 1;
2
+
3
+ events {
4
+ worker_connections 1024;
5
+ }
6
+
7
+ http {
8
+ include mime.types;
9
+ default_type application/octet-stream;
10
+ sendfile on;
11
+ keepalive_timeout 65;
12
+
13
+ map $http_upgrade $connection_upgrade {
14
+ default upgrade;
15
+ '' close;
16
+ }
17
+
18
+ server {
19
+ listen 7860;
20
+ server_name localhost;
21
+
22
+ # 1. OpenEnv API Endpoints (FastAPI on Port 8000)
23
+ location /reset {
24
+ proxy_pass http://localhost:8000/reset;
25
+ proxy_set_header Host $host;
26
+ proxy_set_header X-Real-IP $remote_addr;
27
+ }
28
+
29
+ location /step {
30
+ proxy_pass http://localhost:8000/step;
31
+ proxy_set_header Host $host;
32
+ proxy_set_header X-Real-IP $remote_addr;
33
+ }
34
+
35
+ location /health {
36
+ proxy_pass http://localhost:8000/health;
37
+ proxy_set_header Host $host;
38
+ proxy_set_header X-Real-IP $remote_addr;
39
+ }
40
+
41
+ # 2. Streamlit WebSocket (Port 8501)
42
+ location /_stcore/stream {
43
+ proxy_pass http://localhost:8501/_stcore/stream;
44
+ proxy_http_version 1.1;
45
+ proxy_set_header Upgrade $http_upgrade;
46
+ proxy_set_header Connection "upgrade";
47
+ proxy_set_header Host $host;
48
+ proxy_set_header X-Real-IP $remote_addr;
49
+ proxy_read_timeout 86400;
50
+ }
51
+
52
+ # 3. Streamlit UI (Port 8501)
53
+ location / {
54
+ proxy_pass http://localhost:8501;
55
+ proxy_set_header Host $host;
56
+ proxy_set_header X-Real-IP $remote_addr;
57
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
58
+ proxy_set_header X-Forwarded-Proto $scheme;
59
+
60
+ # Additional Streamlit settings
61
+ proxy_http_version 1.1;
62
+ proxy_set_header Upgrade $http_upgrade;
63
+ proxy_set_header Connection $connection_upgrade;
64
+ }
65
+ }
66
+ }
start.sh CHANGED
@@ -1,13 +1,13 @@
1
  #!/bin/bash
2
 
 
 
 
 
3
  # Start the Streamlit dashboard internally on port 8501
4
  echo "Starting Streamlit Dashboard on port 8501..."
5
  streamlit run app.py --server.port 8501 --server.address 0.0.0.0 --server.headless true &
6
 
7
- # Start the FastAPI Gateway on the primary port 7860
8
- # This gateway handles /reset, /step, and proxies everything else to Streamlit
9
- echo "Starting FastAPI Gateway on port 7860..."
10
- uvicorn gateway:app --host 0.0.0.0 --port 7860 &
11
-
12
- # Wait for all processes to finish
13
- wait
 
1
  #!/bin/bash
2
 
3
+ # Start the FastAPI backend on port 8000
4
+ echo "Starting FastAPI Backend on port 8000..."
5
+ uvicorn server.app:app --host 0.0.0.0 --port 8000 &
6
+
7
  # Start the Streamlit dashboard internally on port 8501
8
  echo "Starting Streamlit Dashboard on port 8501..."
9
  streamlit run app.py --server.port 8501 --server.address 0.0.0.0 --server.headless true &
10
 
11
+ # Start Nginx in the foreground to keep the container alive
12
+ echo "Starting Nginx Proxy on port 7860..."
13
+ nginx -g "daemon off;"