oopshnik commited on
Commit
7d5e051
·
verified ·
1 Parent(s): 5599cad

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +48 -0
main.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, Request, Response
2
+ import httpx
3
+ import uvicorn
4
+
5
+ app = FastAPI()
6
+
7
+ @app.api_route("/", methods=["GET", "POST", "PUT", "DELETE"])
8
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
9
+ async def proxy(request: Request, url: str = Query(...)):
10
+ async with httpx.AsyncClient() as client:
11
+ method = request.method
12
+ headers = dict(request.headers)
13
+ headers.pop("host", None)
14
+
15
+ content = await request.body()
16
+
17
+ try:
18
+ target_response = await client.request(
19
+ method,
20
+ url,
21
+ headers=headers,
22
+ content=content,
23
+ follow_redirects=True
24
+ )
25
+ except Exception as e:
26
+ return Response(content=str(e), status_code=500)
27
+
28
+ excluded_headers = [
29
+ "content-encoding",
30
+ "content-length",
31
+ "transfer-encoding",
32
+ "connection"
33
+ ]
34
+
35
+ response_headers = {
36
+ k: v for k, v in target_response.headers.items()
37
+ if k.lower() not in excluded_headers
38
+ }
39
+
40
+ return Response(
41
+ content=target_response.content,
42
+ status_code=target_response.status_code,
43
+ headers=response_headers,
44
+ media_type=target_response.headers.get("content-type")
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ uvicorn.run(app, host="0.0.0.0", port=8000)