geqintan commited on
Commit
ca00125
·
1 Parent(s): d462ad5
Files changed (2) hide show
  1. app.py +2 -0
  2. routes/v2.py +65 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  # 导入FastAPI框架
2
  from fastapi import FastAPI
3
  from routes import v1
 
4
  # 导入数据库引擎
5
  from database import engine
6
  # 导入用户模型的Base类
@@ -13,6 +14,7 @@ Base.metadata.create_all(bind=engine)
13
  app = FastAPI()
14
 
15
  app.include_router(v1.router, prefix="/v1")
 
16
 
17
  # 定义根路径的GET请求处理函数
18
  @app.get("/")
 
1
  # 导入FastAPI框架
2
  from fastapi import FastAPI
3
  from routes import v1
4
+ from routes import v2
5
  # 导入数据库引擎
6
  from database import engine
7
  # 导入用户模型的Base类
 
14
  app = FastAPI()
15
 
16
  app.include_router(v1.router, prefix="/v1")
17
+ app.include_router(v2.router, prefix="/v2")
18
 
19
  # 定义根路径的GET请求处理函数
20
  @app.get("/")
routes/v2.py CHANGED
@@ -1 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # routes for v2 API
 
1
+ import httpx
2
+ from fastapi import APIRouter, HTTPException, Request
3
+ import json
4
+
5
+ router = APIRouter()
6
+
7
+ @router.get("/")
8
+ async def v2():
9
+ return {"message": "Hello World from v2"}
10
+
11
+ @router.get("/{protocol}/{url:path}")
12
+ async def proxy_request(protocol:str, url: str):
13
+ async with httpx.AsyncClient() as client:
14
+ try:
15
+ target_url = f"{protocol}://{url}"
16
+ response = await client.get(
17
+ target_url,
18
+ headers={},
19
+ )
20
+ response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
21
+ return response.content
22
+ except httpx.HTTPStatusError as e:
23
+ raise HTTPException(status_code=e.response.status_code, detail=str(e))
24
+ except httpx.RequestError as e:
25
+ raise HTTPException(status_code=500, detail=str(e))
26
+
27
+ @router.post("/{protocol}/{url:path}")
28
+ async def proxy_post(protocol:str, url: str, request: Request):
29
+ body = await request.body()
30
+ headers = dict(request.headers) # 将请求头转换为字典
31
+
32
+ # 移除 Content-Length 和 Host 头部
33
+ if 'content-length' in headers:
34
+ del headers['content-length']
35
+ if 'host' in headers:
36
+ del headers['host']
37
+
38
+ headers['User-Agent']='curl/8.7.1'
39
+
40
+ dest_url = f"{protocol}://{url}"
41
+
42
+ async with httpx.AsyncClient() as client:
43
+ try:
44
+ # 向目标 URL 发送 POST 请求
45
+ response = await client.post(dest_url, content=body, headers=headers)
46
+
47
+ # 检查响应状态码
48
+ if response.status_code == 200:
49
+ # 检查响应内容类型是否为 JSON
50
+ if 'application/json' in response.headers.get('content-type', ''):
51
+ return response.json()
52
+ else:
53
+ return {"error": "Response is not in JSON format"}
54
+ else:
55
+ # 将错误响应转换为 JSON 格式并返回给客户端
56
+ try:
57
+ error_data = response.json()
58
+ except ValueError:
59
+ error_data = {"status_code": response.status_code, "detail": response.text}
60
+ return error_data
61
+
62
+ except httpx.RequestError as e:
63
+ # 处理请求错误
64
+ raise HTTPException(status_code=500, detail=str(e))
65
+
66
  # routes for v2 API