|
from fastapi import FastAPI, Request, Response, status |
|
import httpx |
|
import uvicorn |
|
import time |
|
app = FastAPI() |
|
time.sleep(10) |
|
TARGET_BASE = "http://127.0.0.1:8001" |
|
resp = httpx.get(TARGET_BASE) |
|
print(f'RESP:{ resp.json}') |
|
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]) |
|
async def proxy(request: Request, path: str): |
|
resp = httpx.get(TARGET_BASE) |
|
print(f'RESP:{ resp.json}') |
|
|
|
|
|
|
|
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) |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
uvicorn.run("fun:app", host="0.0.0.0", port=8000) |