xidu commited on
Commit
f8d003f
·
1 Parent(s): ed3b061

refactor: Final attempt with pure python gateway

Browse files
Files changed (10) hide show
  1. Dockerfile +6 -20
  2. README.md +5 -5
  3. app_7856.py +0 -17
  4. app_7861.py +1 -1
  5. app_7864.py +1 -1
  6. gateway.py +48 -0
  7. nginx.conf +0 -33
  8. requirements.txt +1 -1
  9. run.sh +10 -0
  10. supervisord.conf +0 -48
Dockerfile CHANGED
@@ -1,30 +1,16 @@
1
  FROM python:3.9-slim
2
 
3
- # Install nginx and supervisor
4
- RUN apt-get update && apt-get install -y nginx supervisor
5
-
6
- # === 关键修正: 创建 Supervisor 日志目录以防止静默启动失败 ===
7
- RUN mkdir -p /var/log/supervisor
8
-
9
- # Set working directory
10
  WORKDIR /app
11
 
12
- # Copy and install Python dependencies first
13
- COPY ./requirements.txt /app/requirements.txt
14
  RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
15
 
16
- # Copy application code
17
- COPY ./*.py /app/
18
-
19
- # Copy configurations to their standard system locations
20
- COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
21
- COPY nginx.conf /etc/nginx/nginx.conf
22
 
23
- # === 关键修正: 确保工作目录权限正确 ===
24
- RUN chown -R www-data:www-data /app
25
 
26
- # Expose the single public port for Nginx
27
  EXPOSE 7860
28
 
29
- # Start supervisor using its default configuration path
30
- CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
 
1
  FROM python:3.9-slim
2
 
 
 
 
 
 
 
 
3
  WORKDIR /app
4
 
5
+ COPY ./requirements.txt /app/
 
6
  RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
7
 
8
+ COPY . /app/
 
 
 
 
 
9
 
10
+ # 赋予启动脚本可执行权限
11
+ RUN chmod +x run.sh
12
 
 
13
  EXPOSE 7860
14
 
15
+ # 将启动脚本作为容器的主命令
16
+ CMD ["/bin/bash", "./run.sh"]
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Multi-Service GenAI API (DEBUG MODE)
3
- emoji: 🐞
4
- colorFrom: yellow
5
- colorTo: red
6
  sdk: docker
7
  pinned: false
8
  ---
9
- This Space is in debug mode. Service `api_56` is temporarily disabled to isolate potential issues.
10
 
11
  - Access service 2 at `/api_61/`
12
  - Access service 3 at `/api_64/`
 
1
  ---
2
+ title: Ultimate Debug v2 (Pure Python)
3
+ emoji:
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: docker
7
  pinned: false
8
  ---
9
+ This is a minimal multi-service setup using a pure Python gateway.
10
 
11
  - Access service 2 at `/api_61/`
12
  - Access service 3 at `/api_64/`
app_7856.py DELETED
@@ -1,17 +0,0 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- import google.generativeai as genai
4
- import os
5
- # genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
6
- genai.configure(api_key="YOUR_GOOGLE_API_KEY_HERE")
7
- model = genai.GenerativeModel('gemini-pro')
8
- app = FastAPI()
9
- class ChatRequest(BaseModel):
10
- prompt: str
11
- @app.post("/chat")
12
- async def chat_handler(request: ChatRequest):
13
- try:
14
- response = model.generate_content(request.prompt)
15
- return {"response": response.text}
16
- except Exception as e:
17
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_7861.py CHANGED
@@ -2,4 +2,4 @@ from fastapi import FastAPI
2
  app = FastAPI()
3
  @app.get("/")
4
  def read_root():
5
- return {"message": "This is Service #2 on Port 7861 (Debug Mode)"}
 
2
  app = FastAPI()
3
  @app.get("/")
4
  def read_root():
5
+ return {"message": "Response from Service #2 on Port 7861"}
app_7864.py CHANGED
@@ -2,4 +2,4 @@ from fastapi import FastAPI
2
  app = FastAPI()
3
  @app.get("/")
4
  def read_root():
5
- return {"message": "This is Service #3 on Port 7864 (Debug Mode)"}
 
2
  app = FastAPI()
3
  @app.get("/")
4
  def read_root():
5
+ return {"message": "Response from Service #3 on Port 7864"}
gateway.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ from fastapi import FastAPI, Request, Response
3
+
4
+ app = FastAPI()
5
+
6
+ # 定义服务名称到其内部地址的映射
7
+ SERVICE_MAP = {
8
+ "api_61": "http://127.0.0.1:7861",
9
+ "api_64": "http://127.0.0.1:7864",
10
+ }
11
+
12
+ @app.api_route("/{service_path:path}")
13
+ async def reverse_proxy(request: Request, service_path: str):
14
+ # 从 URL 路径中解析出服务名称
15
+ # e.g., /api_61/some/other/path -> "api_61"
16
+ path_parts = service_path.split('/')
17
+ service_name = path_parts[0]
18
+
19
+ if service_name in SERVICE_MAP:
20
+ service_url = SERVICE_MAP[service_name]
21
+
22
+ # 重新构建指向内部服务的完整 URL
23
+ rest_of_path = "/".join(path_parts[1:])
24
+ target_url = f"{service_url}/{rest_of_path}"
25
+
26
+ # 使用 httpx 异步转发请求
27
+ async with httpx.AsyncClient() as client:
28
+ backend_request = client.build_request(
29
+ method=request.method,
30
+ url=target_url,
31
+ headers=request.headers,
32
+ params=request.query_params,
33
+ content=await request.body()
34
+ )
35
+
36
+ # 发送请求到后端服务
37
+ try:
38
+ backend_response = await client.send(backend_request, timeout=300.0)
39
+ # 将后端服务的响应原样返回给客户端
40
+ return Response(
41
+ content=backend_response.content,
42
+ status_code=backend_response.status_code,
43
+ headers=dict(backend_response.headers),
44
+ )
45
+ except httpx.ConnectError as e:
46
+ return Response(content=f"Could not connect to service '{service_name}'. Is it running?", status_code=502)
47
+
48
+ return Response(content=f"Service '{service_name}' not found.", status_code=404)
nginx.conf DELETED
@@ -1,33 +0,0 @@
1
- user www-data;
2
- worker_processes auto;
3
- pid /run/nginx.pid;
4
- include /etc/nginx/modules-enabled/*.conf;
5
-
6
- events {
7
- worker_connections 768;
8
- }
9
-
10
- http {
11
- server {
12
- listen 7860;
13
- server_name localhost;
14
-
15
- client_max_body_size 50M;
16
- proxy_connect_timeout 300s;
17
- proxy_read_timeout 300s;
18
- proxy_send_timeout 300s;
19
-
20
- # 注意:/api_56/ 的路由仍然保留,但会返回 502 Bad Gateway,这是正常的
21
- location /api_56/ {
22
- proxy_pass http://127.0.0.1:7856/;
23
- }
24
-
25
- location /api_61/ {
26
- proxy_pass http://127.0.0.1:7861/;
27
- }
28
-
29
- location /api_64/ {
30
- proxy_pass http://127.0.0.1:7864/;
31
- }
32
- }
33
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  fastapi
2
  uvicorn
3
- google-generativeai
 
1
  fastapi
2
  uvicorn
3
+ httpx
run.sh ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 在后台启动所有子服务
4
+ echo "Starting background services..."
5
+ uvicorn app_7861:app --host 0.0.0.0 --port 7861 &
6
+ uvicorn app_7864:app --host 0.0.0.0 --port 7864 &
7
+
8
+ # 在前台启动主网关服务,这个进程将保持容器运行
9
+ echo "Starting main gateway on port 7860..."
10
+ uvicorn gateway:app --host 0.0.0.0 --port 7860
supervisord.conf DELETED
@@ -1,48 +0,0 @@
1
- [supervisord]
2
- nodaemon=true
3
- logfile=/var/log/supervisor/supervisord.log
4
- pidfile=/var/run/supervisord.pid
5
-
6
- [program:nginx]
7
- command=/usr/sbin/nginx -g 'daemon off;'
8
- user=root
9
- autostart=true
10
- autorestart=true
11
- stdout_logfile=/dev/stdout
12
- stdout_logfile_maxbytes=0
13
- stderr_logfile=/dev/stderr
14
- stderr_logfile_maxbytes=0
15
-
16
- ; === FOR DEBUGGING: The following service is temporarily disabled ===
17
- ;[program:app_7856]
18
- ;command=uvicorn app_7856:app --host 0.0.0.0 --port 7856
19
- ;directory=/app
20
- ;user=root
21
- ;autostart=true
22
- ;autorestart=true
23
- ;stdout_logfile=/dev/stdout
24
- ;stdout_logfile_maxbytes=0
25
- ;stderr_logfile=/dev/stderr
26
- ;stderr_logfile_maxbytes=0
27
-
28
- [program:app_7861]
29
- command=uvicorn app_7861:app --host 0.0.0.0 --port 7861
30
- directory=/app
31
- user=root
32
- autostart=true
33
- autorestart=true
34
- stdout_logfile=/dev/stdout
35
- stdout_logfile_maxbytes=0
36
- stderr_logfile=/dev/stderr
37
- stderr_logfile_maxbytes=0
38
-
39
- [program:app_7864]
40
- command=uvicorn app_7864:app --host 0.0.0.0 --port 7864
41
- directory=/app
42
- user=root
43
- autostart=true
44
- autorestart=true
45
- stdout_logfile=/dev/stdout
46
- stdout_logfile_maxbytes=0
47
- stderr_logfile=/dev/stderr
48
- stderr_logfile_maxbytes=0