|
from fastapi import FastAPI, HTTPException, Request |
|
from fastapi.middleware.cors import CORSMiddleware |
|
import requests |
|
|
|
app = FastAPI() |
|
|
|
|
|
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") |
|
|
|
|
|
if request.method == "GET": |
|
response = requests.get(url) |
|
elif request.method == "POST": |
|
|
|
data = await request.json() if request.headers.get("Content-Type") == "application/json" else None |
|
response = requests.post(url, json=data) |
|
|
|
|
|
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) |
|
|