proxyv1.2 / main.py
opencode
perf: use singleton httpx client to reduce RAM usage
fc4195e
Raw
History Blame Contribute Delete
1.77 kB
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import httpx
from contextlib import asynccontextmanager
# The target Azure VM backend URL
TARGET_URL = "https://snap-providers-mercy-protocol.trycloudflare.com"
# Use a global client to avoid creating a new one for every request (reduces RAM/CPU overhead)
client_state = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize the client on startup
client_state["client"] = httpx.AsyncClient(timeout=60.0)
yield
# Close the client on shutdown
await client_state["client"].aclose()
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
async def proxy(request: Request, path: str):
\"\"\"Forwards all requests to the target backend.\"\"\"
url = f"{TARGET_URL}/{path}"
if request.method == "OPTIONS":
return Response(status_code=200)
client = client_state["client"]
body = await request.body()
headers = dict(request.headers)
headers.pop("host", None)
try:
resp = await client.request(
method=request.method,
url=url,
headers=headers,
content=body
)
return Response(
content=resp.content,
status_code=resp.status_code,
headers=dict(resp.headers)
)
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Proxy Error: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)