Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,39 @@
|
|
| 1 |
-
import
|
| 2 |
-
from fastapi import
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
-
fastapi_app = FastAPI()
|
| 7 |
|
| 8 |
-
@
|
| 9 |
-
|
|
|
|
| 10 |
try:
|
| 11 |
# 构建目标 URL
|
| 12 |
-
if
|
| 13 |
-
|
| 14 |
-
actual_path = full_path[4:] # 跳过 "api/"
|
| 15 |
target_url = f"http://beibeioo.top/{actual_path}"
|
| 16 |
else:
|
| 17 |
-
# 否则直接访问根路径
|
| 18 |
target_url = "http://beibeioo.top"
|
| 19 |
-
|
| 20 |
# 转发请求
|
| 21 |
-
response = requests.get(
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
-
return
|
| 25 |
-
|
| 26 |
-
# 创建 Gradio 接口
|
| 27 |
-
demo = gr.Interface(
|
| 28 |
-
fn=lambda x: "API Proxy is running",
|
| 29 |
-
inputs=None,
|
| 30 |
-
outputs="text",
|
| 31 |
-
title="API Proxy",
|
| 32 |
-
description="Access beibeioo.top through this proxy"
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# 重要:使用 Gradio 的 app 属性
|
| 36 |
-
app = demo.app
|
| 37 |
-
|
| 38 |
-
# 将 FastAPI 路由添加到 Gradio 的 app 中
|
| 39 |
-
for route in fastapi_app.routes:
|
| 40 |
-
app.add_route(route.path, route.endpoint, methods=route.methods)
|
| 41 |
-
|
| 42 |
-
if __name__ == "__main__":
|
| 43 |
-
demo.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import Response, HTMLResponse
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
| 6 |
|
| 7 |
+
@app.get("/", response_class=HTMLResponse)
|
| 8 |
+
@app.get("/{path:path}", response_class=HTMLResponse)
|
| 9 |
+
async def proxy(request: Request, path: str = ""):
|
| 10 |
try:
|
| 11 |
# 构建目标 URL
|
| 12 |
+
if path.startswith("api/"):
|
| 13 |
+
actual_path = path[4:] # 跳过 "api/"
|
|
|
|
| 14 |
target_url = f"http://beibeioo.top/{actual_path}"
|
| 15 |
else:
|
|
|
|
| 16 |
target_url = "http://beibeioo.top"
|
| 17 |
+
|
| 18 |
# 转发请求
|
| 19 |
+
response = requests.get(
|
| 20 |
+
target_url,
|
| 21 |
+
headers={
|
| 22 |
+
'User-Agent': 'Mozilla/5.0',
|
| 23 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 24 |
+
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
| 25 |
+
}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# 返回响应
|
| 29 |
+
return Response(
|
| 30 |
+
content=response.content,
|
| 31 |
+
media_type=response.headers.get('content-type', 'text/html'),
|
| 32 |
+
headers={
|
| 33 |
+
'Access-Control-Allow-Origin': '*',
|
| 34 |
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
| 35 |
+
'Access-Control-Allow-Headers': '*'
|
| 36 |
+
}
|
| 37 |
+
)
|
| 38 |
except Exception as e:
|
| 39 |
+
return HTMLResponse(content=f"Error: {str(e)}", status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|