Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Request
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
import requests
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Add CORS middleware to allow all origins. Adjust this in production!
|
8 |
+
app.add_middleware(
|
9 |
+
CORSMiddleware,
|
10 |
+
allow_origins=["*"],
|
11 |
+
allow_credentials=True,
|
12 |
+
allow_methods=["*"],
|
13 |
+
allow_headers=["*"],
|
14 |
+
)
|
15 |
+
|
16 |
+
@app.get("/proxy/")
|
17 |
+
@app.post("/proxy/")
|
18 |
+
async def proxy(request: Request, url: str):
|
19 |
+
if not url:
|
20 |
+
raise HTTPException(status_code=400, detail="URL not provided")
|
21 |
+
|
22 |
+
# Forward the request to the target URL
|
23 |
+
if request.method == "GET":
|
24 |
+
response = requests.get(url)
|
25 |
+
elif request.method == "POST":
|
26 |
+
# Read JSON data if available
|
27 |
+
data = await request.json() if request.headers.get("Content-Type") == "application/json" else None
|
28 |
+
response = requests.post(url, json=data)
|
29 |
+
|
30 |
+
# Return the response from the target server
|
31 |
+
return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers))
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
import uvicorn
|
35 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|