Update run.py
Browse files
run.py
CHANGED
|
@@ -1,6 +1,27 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
app = reverse_http_app(base_url="http://www.example.com/foo/")
|
| 4 |
if __name__ == '__main__':
|
| 5 |
import uvicorn
|
| 6 |
uvicorn.run("run:app", host="0.0.0.0", port="8080")
|
|
|
|
| 1 |
+
from contextlib import asynccontextmanager
|
| 2 |
+
from typing import AsyncIterator
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi_proxy_lib.core.http import ForwardHttpProxy
|
| 6 |
+
from fastapi_proxy_lib.core.tool import default_proxy_filter
|
| 7 |
+
from httpx import AsyncClient
|
| 8 |
+
from starlette.requests import Request
|
| 9 |
+
|
| 10 |
+
proxy = ForwardHttpProxy(AsyncClient(), proxy_filter=default_proxy_filter)
|
| 11 |
+
|
| 12 |
+
@asynccontextmanager
|
| 13 |
+
async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
|
| 14 |
+
"""Close proxy."""
|
| 15 |
+
yield
|
| 16 |
+
await proxy.aclose()
|
| 17 |
+
|
| 18 |
+
app = FastAPI(lifespan=close_proxy_event)
|
| 19 |
+
|
| 20 |
+
@app.get("/{path:path}")
|
| 21 |
+
async def _(request: Request, path: str = ""):
|
| 22 |
+
return await proxy.proxy(request=request, path=path)
|
| 23 |
+
|
| 24 |
|
|
|
|
| 25 |
if __name__ == '__main__':
|
| 26 |
import uvicorn
|
| 27 |
uvicorn.run("run:app", host="0.0.0.0", port="8080")
|