api / app.py
bobocup's picture
Update app.py
cb2432b verified
raw
history blame
1.35 kB
from fastapi import FastAPI, Request
from fastapi.responses import Response, HTMLResponse
import requests
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
@app.get("/{path:path}", response_class=HTMLResponse)
async def proxy(request: Request, path: str = ""):
try:
# 构建目标 URL
if path.startswith("api/"):
actual_path = path[4:] # 跳过 "api/"
target_url = f"http://beibeioo.top/{actual_path}"
else:
target_url = "http://beibeioo.top"
# 转发请求
response = requests.get(
target_url,
headers={
'User-Agent': 'Mozilla/5.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
)
# 返回响应
return Response(
content=response.content,
media_type=response.headers.get('content-type', 'text/html'),
headers={
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': '*'
}
)
except Exception as e:
return HTMLResponse(content=f"Error: {str(e)}", status_code=500)