File size: 2,884 Bytes
4f8c3bf 50cf058 ec1323a 50cf058 4f8c3bf ec1323a 4f8c3bf ec1323a 4f8c3bf ec1323a |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from fastapi import APIRouter, Request, HTTPException, Response
import httpx
LARAVEL_URL = "http://localhost:8000"
router = APIRouter(prefix="/gradios", tags=["gradios"])
# GET
@router.get("/route/{path:path}")
async def proxy_get(path: str, request: Request):
async with httpx.AsyncClient() as client:
headers = dict(request.headers)
try:
proxied = await client.get(f"{LARAVEL_URL}/{path}", headers=headers)
return Response(
content=proxied.content,
status_code=proxied.status_code,
headers=dict(proxied.headers),
media_type=proxied.headers.get("content-type")
)
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Request proxy failed: {str(e)}")
# POST
@router.post("/route/{path:path}")
async def proxy_post(path: str, request: Request):
async with httpx.AsyncClient() as client:
req_data = await request.body()
headers = dict(request.headers)
try:
proxied = await client.post(f"{LARAVEL_URL}/{path}", headers=headers, content=req_data)
return Response(
content=proxied.content,
status_code=proxied.status_code,
headers=dict(proxied.headers),
media_type=proxied.headers.get("content-type")
)
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Request proxy failed: {str(e)}")
# PUT
@router.put("/route/{path:path}")
async def proxy_put(path: str, request: Request):
async with httpx.AsyncClient() as client:
req_data = await request.body()
headers = dict(request.headers)
try:
proxied = await client.put(f"{LARAVEL_URL}/{path}", headers=headers, content=req_data)
return Response(
content=proxied.content,
status_code=proxied.status_code,
headers=dict(proxied.headers),
media_type=proxied.headers.get("content-type")
)
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Request proxy failed: {str(e)}")
# DELETE
@router.delete("/route/{path:path}")
async def proxy_delete(path: str, request: Request):
async with httpx.AsyncClient() as client:
headers = dict(request.headers)
try:
proxied = await client.delete(f"{LARAVEL_URL}/{path}", headers=headers)
return Response(
content=proxied.content,
status_code=proxied.status_code,
headers=dict(proxied.headers),
media_type=proxied.headers.get("content-type")
)
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Request proxy failed: {str(e)}")
|