File size: 1,128 Bytes
5fa8a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
import requests

app = FastAPI()

# Add CORS middleware to allow all origins. Adjust this in production!
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/proxy/")
@app.post("/proxy/")
async def proxy(request: Request, url: str):
    if not url:
        raise HTTPException(status_code=400, detail="URL not provided")

    # Forward the request to the target URL
    if request.method == "GET":
        response = requests.get(url)
    elif request.method == "POST":
        # Read JSON data if available
        data = await request.json() if request.headers.get("Content-Type") == "application/json" else None
        response = requests.post(url, json=data)

    # Return the response from the target server
    return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)