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
| import httpx
from fastapi import FastAPI, Request
from starlette.requests import Request
from starlette.responses import StreamingResponse
from starlette.background import BackgroundTask
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
proxy_base_host = "localhost"
proxy_base_port = 8000
client = httpx.AsyncClient(base_url=f"http://{proxy_base_host}:{proxy_base_port}")
async def _reverse_proxy(request: Request):
url = httpx.URL(path=request.url.path,
query=request.url.query.encode("utf-8"))
_headers = {
**request.headers,
"host" : proxy_base_host
}
rp_req = client.build_request(request.method, url,
headers=_headers,
content=await request.body())
rp_resp = await client.send(rp_req, stream=True)
return StreamingResponse(
rp_resp.aiter_raw(),
status_code=rp_resp.status_code,
headers=rp_resp.headers,
background=BackgroundTask(rp_resp.aclose),
)
app.add_route("/{path:path}",
_reverse_proxy, ["GET", "POST"])
|