Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI, Request, Response
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import httpx
|
|
|
|
| 5 |
from typing import Optional
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
@@ -23,6 +24,13 @@ async def get_http_client():
|
|
| 23 |
http2=True
|
| 24 |
)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
async def proxy_handler(url: str, request: Request):
|
| 27 |
try:
|
| 28 |
headers = dict(request.headers)
|
|
@@ -35,25 +43,58 @@ async def proxy_handler(url: str, request: Request):
|
|
| 35 |
body = await request.body()
|
| 36 |
|
| 37 |
async with await get_http_client() as client:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except httpx.TimeoutException:
|
| 59 |
return Response(content="请求超时", status_code=504)
|
|
@@ -71,7 +112,6 @@ async def api_v1_proxy(path: str, request: Request):
|
|
| 71 |
# 处理根路径和其他路径
|
| 72 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 73 |
async def root_proxy(path: str, request: Request):
|
| 74 |
-
# 如果是根路径或空路径
|
| 75 |
if not path or path == "/":
|
| 76 |
url = TARGET_URL
|
| 77 |
else:
|
|
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import httpx
|
| 5 |
+
import json
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
|
|
| 24 |
http2=True
|
| 25 |
)
|
| 26 |
|
| 27 |
+
async def is_stream_request(request: Request) -> bool:
|
| 28 |
+
# 检查请求头中的 accept 字段
|
| 29 |
+
accept = request.headers.get('accept', '')
|
| 30 |
+
# 检查查询参数
|
| 31 |
+
stream = request.query_params.get('stream', '').lower()
|
| 32 |
+
return 'text/event-stream' in accept or stream == 'true'
|
| 33 |
+
|
| 34 |
async def proxy_handler(url: str, request: Request):
|
| 35 |
try:
|
| 36 |
headers = dict(request.headers)
|
|
|
|
| 43 |
body = await request.body()
|
| 44 |
|
| 45 |
async with await get_http_client() as client:
|
| 46 |
+
# 判断是否需要流式传输
|
| 47 |
+
if await is_stream_request(request):
|
| 48 |
+
# 流式请求
|
| 49 |
+
response = await client.stream(
|
| 50 |
+
method=request.method,
|
| 51 |
+
url=url,
|
| 52 |
+
params=params,
|
| 53 |
+
headers=headers,
|
| 54 |
+
content=body if body else None,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
response_headers = dict(response.headers)
|
| 58 |
+
response_headers.pop('transfer-encoding', None)
|
| 59 |
+
response_headers.pop('content-encoding', None)
|
| 60 |
+
response_headers.pop('content-length', None)
|
| 61 |
+
|
| 62 |
+
async def stream_generator():
|
| 63 |
+
try:
|
| 64 |
+
async for chunk in response.aiter_bytes():
|
| 65 |
+
yield chunk
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Streaming error: {e}")
|
| 68 |
+
finally:
|
| 69 |
+
await response.aclose()
|
| 70 |
+
|
| 71 |
+
return StreamingResponse(
|
| 72 |
+
stream_generator(),
|
| 73 |
+
status_code=response.status_code,
|
| 74 |
+
headers=response_headers,
|
| 75 |
+
media_type=response.headers.get('content-type')
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
# 非流式请求
|
| 79 |
+
response = await client.request(
|
| 80 |
+
method=request.method,
|
| 81 |
+
url=url,
|
| 82 |
+
params=params,
|
| 83 |
+
headers=headers,
|
| 84 |
+
content=body if body else None,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
response_headers = dict(response.headers)
|
| 88 |
+
response_headers.pop('transfer-encoding', None)
|
| 89 |
+
response_headers.pop('content-encoding', None)
|
| 90 |
+
response_headers.pop('content-length', None)
|
| 91 |
+
|
| 92 |
+
return Response(
|
| 93 |
+
content=response.content,
|
| 94 |
+
status_code=response.status_code,
|
| 95 |
+
headers=response_headers,
|
| 96 |
+
media_type=response.headers.get('content-type')
|
| 97 |
+
)
|
| 98 |
|
| 99 |
except httpx.TimeoutException:
|
| 100 |
return Response(content="请求超时", status_code=504)
|
|
|
|
| 112 |
# 处理根路径和其他路径
|
| 113 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 114 |
async def root_proxy(path: str, request: Request):
|
|
|
|
| 115 |
if not path or path == "/":
|
| 116 |
url = TARGET_URL
|
| 117 |
else:
|