proxy-agents / app.py
Adilmar's picture
Update app.py
c8ecf05 verified
raw
history blame
1.29 kB
from fastapi import FastAPI, Request, Response
import httpx
import os
app = FastAPI()
BACKEND_URL = os.environ.get("BACKEND_URL")
AUTH_HEADER = os.environ.get("AUTH_HEADER")
@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
async def proxy(full_path: str, request: Request):
url = f"{BACKEND_URL}/{full_path}"
# Copia headers originais e adiciona Authorization
headers = dict(request.headers)
headers["Authorization"] = AUTH_HEADER
# Remove headers que podem causar conflito
for h in ["host", "content-length", "accept-encoding", "connection"]:
headers.pop(h, None)
body = await request.body()
async with httpx.AsyncClient() as client:
resp = await client.request(
method=request.method,
url=url,
headers=headers,
content=body if request.method != "GET" else None,
params=dict(request.query_params)
)
# Retorna resposta do backend
return Response(
content=resp.content,
status_code=resp.status_code,
headers={k: v for k, v in resp.headers.items() if k.lower() not in ["content-encoding", "transfer-encoding", "connection"]}
)
# Para rodar:
# uvicorn proxy:app --reload --port 8000