Update main.py
Browse files
main.py
CHANGED
@@ -1,32 +1,33 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
-
from fastapi.responses import
|
3 |
import httpx
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
|
8 |
async def proxy(request: Request, path: str):
|
|
|
|
|
9 |
async with httpx.AsyncClient() as client:
|
10 |
-
# 使用相同的方法和路径参数发出请求
|
11 |
method = request.method
|
12 |
-
url = f"http://52.91.103.39:1234/{path}"
|
13 |
-
|
14 |
-
# 转发请求头(可选)
|
15 |
headers = dict(request.headers)
|
16 |
-
|
17 |
-
# 转发请求体(如果有的话)
|
18 |
content = await request.body()
|
19 |
-
|
20 |
-
# 执行代理请求
|
21 |
response = await client.request(
|
22 |
method=method,
|
23 |
-
url=
|
24 |
headers=headers,
|
|
|
25 |
content=content
|
26 |
)
|
27 |
-
|
28 |
-
# 返回相同的响应内容和状态码
|
29 |
-
return JSONResponse(content=response.json(), status_code=response.status_code)
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import Response
|
3 |
import httpx
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
|
8 |
async def proxy(request: Request, path: str):
|
9 |
+
target_url = f"http://52.91.103.39:1234/{path}"
|
10 |
+
|
11 |
async with httpx.AsyncClient() as client:
|
|
|
12 |
method = request.method
|
|
|
|
|
|
|
13 |
headers = dict(request.headers)
|
14 |
+
params = request.query_params
|
|
|
15 |
content = await request.body()
|
16 |
+
|
|
|
17 |
response = await client.request(
|
18 |
method=method,
|
19 |
+
url=target_url,
|
20 |
headers=headers,
|
21 |
+
params=params,
|
22 |
content=content
|
23 |
)
|
|
|
|
|
|
|
24 |
|
25 |
+
return Response(
|
26 |
+
content=response.content,
|
27 |
+
status_code=response.status_code,
|
28 |
+
headers=dict(response.headers)
|
29 |
+
)
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
import os
|
33 |
+
os.system("uvicorn main:app --host 0.0.0.0 --port 7860")
|