File size: 1,188 Bytes
b137e67 5fa8a0c ac6ba74 5fa8a0c ac6ba74 5fa8a0c b137e67 f196a8a ac6ba74 f196a8a ac6ba74 b137e67 ac6ba74 e1c331f a2ea771 ac6ba74 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, Path
from fastapi.middleware.cors import CORSMiddleware
import httpx
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/{url:path}")
async def proxy(url: str = Path(..., title="The URL to be proxied", description="The entire URL to be proxied")):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
# Ensure the URL has a valid format
if not url.startswith(('http://', 'https://')):
raise HTTPException(status_code=400, detail="Invalid URL format")
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers)
return response.json() # Parse the content as JSON and return
except (httpx.RequestError, ValueError) as e:
raise HTTPException(status_code=500, detail="Unexpected error")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
|