File size: 928 Bytes
0bc2ea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.all("/{path:path}")
async def proxy(request: Request, path: str):
    async with httpx.AsyncClient() as client:
        # 使用相同的方法和路径参数发出请求
        method = request.method
        url = f"http://52.91.103.39:1234/{path}"
        
        # 转发请求头(可选)
        headers = dict(request.headers)
        
        # 转发请求体(如果有的话)
        content = await request.body()
        
        # 执行代理请求
        response = await client.request(
            method=method,
            url=url,
            headers=headers,
            content=content
        )
        
        # 返回相同的响应内容和状态码
        return JSONResponse(content=response.json(), status_code=response.status_code)

# 启动FastAPI应用
# uvicorn main:app --reload