roseforyou commited on
Commit
b39a46e
·
verified ·
1 Parent(s): ef5e3e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import logging
3
+ import os
4
+ from contextlib import asynccontextmanager
5
+ from fastapi import FastAPI, Request
6
+ from fastapi.responses import StreamingResponse
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ PROXY_URL = os.getenv("PROXY_URL", "https://api.x.ai")
12
+ CONNECT_TIMEOUT = float(os.getenv("CONNECT_TIMEOUT", "5.0"))
13
+ READ_TIMEOUT = float(os.getenv("READ_TIMEOUT", "60.0"))
14
+ WRITE_TIMEOUT = float(os.getenv("WRITE_TIMEOUT", "30.0"))
15
+ POOL_TIMEOUT = float(os.getenv("POOL_TIMEOUT", "5.0"))
16
+ MAX_CONNECTIONS = int(os.getenv("MAX_CONNECTIONS", "100"))
17
+ MAX_KEEPALIVE = int(os.getenv("MAX_KEEPALIVE", "20"))
18
+
19
+ client: httpx.AsyncClient
20
+
21
+ @asynccontextmanager
22
+ async def lifespan(app: FastAPI):
23
+ global client
24
+ limits = httpx.Limits(max_connections=MAX_CONNECTIONS, max_keepalive_connections=MAX_KEEPALIVE)
25
+ timeout_config = httpx.Timeout(connect=CONNECT_TIMEOUT, read=READ_TIMEOUT, write=WRITE_TIMEOUT, pool=POOL_TIMEOUT)
26
+ client = httpx.AsyncClient(base_url=PROXY_URL, limits=limits, timeout=timeout_config, http2=True)
27
+ logger.info(f"启动 httpx 客户端,目标: {PROXY_URL}")
28
+ yield
29
+ await client.aclose()
30
+ logger.info("关闭 httpx 客户端")
31
+
32
+ app = FastAPI(lifespan=lifespan)
33
+
34
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
35
+ async def proxy(request: Request, path: str):
36
+ url = httpx.URL(path=f"/{path}", query=request.url.query.encode("utf-8"))
37
+ target_url_log = client.base_url.join(url)
38
+
39
+ logger.info(f"收到请求: {request.method} {request.url}")
40
+ logger.info(f"转发目标: {request.method} {target_url_log}")
41
+
42
+ headers_to_forward = {
43
+ key: value
44
+ for key, value in request.headers.items()
45
+ if key.lower() not in ["host", "connection", "transfer-encoding", "content-length"]
46
+ }
47
+
48
+ request_body = await request.body()
49
+
50
+ try:
51
+ proxy_request = client.build_request(
52
+ method=request.method,
53
+ url=url,
54
+ headers=headers_to_forward,
55
+ content=request_body,
56
+ )
57
+ response = await client.send(proxy_request, stream=True)
58
+
59
+ except httpx.TimeoutException:
60
+ logger.error(f"请求 Proxy API 超时: {request.method} {target_url_log}")
61
+ error_content = b'{"error": {"code": 504, "message": "Gateway Timeout"}}'
62
+ return StreamingResponse(content=error_content, status_code=504, media_type="application/json")
63
+ except httpx.RequestError as e:
64
+ logger.error(f"请求 Proxy API 出错: {request.method} {target_url_log}, Error: {e.__class__.__name__} - {e}")
65
+ error_content = b'{"error": {"code": 502, "message": "Bad Gateway"}}'
66
+ return StreamingResponse(content=error_content, status_code=502, media_type="application/json")
67
+ except Exception as e:
68
+ logger.error(f"代理内部错误: {request.method} {target_url_log}, Error: {e.__class__.__name__} - {e}", exc_info=True)
69
+ error_content = b'{"error": {"code": 500, "message": "Internal Server Error"}}'
70
+ return StreamingResponse(content=error_content, status_code=500, media_type="application/json")
71
+
72
+ response_headers_to_forward = {
73
+ key: value
74
+ for key, value in response.headers.items()
75
+ if key.lower() not in ["content-encoding", "transfer-encoding", "connection"]
76
+ }
77
+
78
+ return StreamingResponse(
79
+ content=response.aiter_raw(),
80
+ status_code=response.status_code,
81
+ headers=response_headers_to_forward,
82
+ media_type=response.headers.get("content-type")
83
+ )
84
+
85
+ @app.get("/")
86
+ async def root():
87
+ return {"message": "Generic Proxy is running. Use /{path} to forward requests."}
88
+
89
+ if __name__ == "__main__":
90
+ import uvicorn
91
+ port = int(os.getenv("PORT", "7860"))
92
+ host = os.getenv("HOST", "0.0.0.0")
93
+ log_level = os.getenv("LOG_LEVEL", "info").lower()
94
+ uvicorn.run(app, host=host, port=port, log_level=log_level)