|
from fastapi import FastAPI, Request, Response, status |
|
import httpx |
|
|
|
app = FastAPI() |
|
TARGET_BASE = "http://127.0.0.1:8001" |
|
|
|
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]) |
|
async def proxy(request: Request, path: str): |
|
if path == "": |
|
|
|
return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND) |
|
url = f"{TARGET_BASE}/{path}" |
|
headers = dict(request.headers) |
|
body = await request.body() |
|
async with httpx.AsyncClient(follow_redirects=True) as client: |
|
resp = await client.request( |
|
request.method, |
|
url, |
|
headers=headers, |
|
content=body, |
|
params=request.query_params |
|
) |
|
return Response( |
|
content=resp.content, |
|
status_code=resp.status_code, |
|
headers=dict(resp.headers) |
|
) |