Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials # 导入HT
|
|
| 6 |
import httpx # 使用httpx替代requests,因为requests是同步的,而FastAPI是异步的
|
| 7 |
import os
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
| 9 |
|
| 10 |
# 加载环境变量
|
| 11 |
load_dotenv()
|
|
@@ -16,6 +17,11 @@ PROXY_API_KEY = os.getenv("PROXY_API_KEY")
|
|
| 16 |
# 定义HTTPBearer安全方案
|
| 17 |
security = HTTPBearer()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# 依赖函数:验证代理的API密钥
|
| 20 |
def verify_proxy_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 21 |
if not PROXY_API_KEY or credentials.credentials != PROXY_API_KEY:
|
|
@@ -44,10 +50,20 @@ GEMINI_BASE_URL = os.getenv("GEMINI_BASE_URL") # 从环境变量获取Gemini真
|
|
| 44 |
# print(f"Using Gemini API Key: {GEMINI_API_KEY}")
|
| 45 |
|
| 46 |
# 2. 通用转发接口(匹配Gemini的/predict等端点,路径动态匹配)
|
| 47 |
-
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 48 |
-
async def proxy_gemini(request: Request, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
|
| 49 |
# 拼接Gemini真实请求URL
|
| 50 |
gemini_url = f"{GEMINI_BASE_URL}/{path}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# 提取客户端请求的headers和body,透传给Gemini
|
| 53 |
client_headers = dict(request.headers)
|
|
@@ -70,7 +86,8 @@ async def proxy_gemini(request: Request, path: str, proxy_api_key: str = Depends
|
|
| 70 |
async with httpx.AsyncClient() as client:
|
| 71 |
response = await client.request(
|
| 72 |
method=request.method,
|
| 73 |
-
url=gemini_url,
|
|
|
|
| 74 |
headers=client_headers,
|
| 75 |
content=client_body, # httpx使用content参数传递请求体
|
| 76 |
timeout=30 # 超时时间,可调整
|
|
|
|
| 6 |
import httpx # 使用httpx替代requests,因为requests是同步的,而FastAPI是异步的
|
| 7 |
import os
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
from enum import Enum
|
| 10 |
|
| 11 |
# 加载环境变量
|
| 12 |
load_dotenv()
|
|
|
|
| 17 |
# 定义HTTPBearer安全方案
|
| 18 |
security = HTTPBearer()
|
| 19 |
|
| 20 |
+
# 枚举校验协议类型
|
| 21 |
+
class ProtocolType(str, Enum):
|
| 22 |
+
HTTP = "http"
|
| 23 |
+
HTTPS = "https"
|
| 24 |
+
|
| 25 |
# 依赖函数:验证代理的API密钥
|
| 26 |
def verify_proxy_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 27 |
if not PROXY_API_KEY or credentials.credentials != PROXY_API_KEY:
|
|
|
|
| 50 |
# print(f"Using Gemini API Key: {GEMINI_API_KEY}")
|
| 51 |
|
| 52 |
# 2. 通用转发接口(匹配Gemini的/predict等端点,路径动态匹配)
|
| 53 |
+
@app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 54 |
+
async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
|
| 55 |
# 拼接Gemini真实请求URL
|
| 56 |
gemini_url = f"{GEMINI_BASE_URL}/{path}"
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
print('gemini_url', gemini_url)
|
| 60 |
+
|
| 61 |
+
print('protocol.value', protocol.value)
|
| 62 |
+
print('host', host)
|
| 63 |
+
print('path', path)
|
| 64 |
+
|
| 65 |
+
real_url = f"{protocol.value}://{host}/{path}"
|
| 66 |
+
print('real_url', real_url)
|
| 67 |
|
| 68 |
# 提取客户端请求的headers和body,透传给Gemini
|
| 69 |
client_headers = dict(request.headers)
|
|
|
|
| 86 |
async with httpx.AsyncClient() as client:
|
| 87 |
response = await client.request(
|
| 88 |
method=request.method,
|
| 89 |
+
# url=gemini_url,
|
| 90 |
+
url=real_url,
|
| 91 |
headers=client_headers,
|
| 92 |
content=client_body, # httpx使用content参数传递请求体
|
| 93 |
timeout=30 # 超时时间,可调整
|