Spaces:
BG5
/
Running

File size: 919 Bytes
4c34c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from fastapi import FastAPI, Request, Response, status
import httpx

app = FastAPI()
TARGET_BASE = "http://127.0.0.1:8001"  # 替换为你的目标服务器

@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def proxy(request: Request, path: str):
    if path == "":
        # 拒绝根目录
        return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND)
    url = f"{TARGET_BASE}/{path}"
    headers = dict(request.headers)
    body = await request.body()
    async with httpx.AsyncClient(follow_redirects=True) as client:
        resp = await client.request(
            request.method,
            url,
            headers=headers,
            content=body,
            params=request.query_params
        )
    return Response(
        content=resp.content,
        status_code=resp.status_code,
        headers=dict(resp.headers)
    )