ProxyModAI / app.py
Sk0lovek's picture
Update app.py
4952bf4 verified
Raw
History Blame Contribute Delete
1.39 kB
from fastapi import FastAPI, Request, Response
import httpx
app = FastAPI()
GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(path: str, request: Request):
async with httpx.AsyncClient() as client:
url = f"{GOOGLE_API_URL}/{path}"
if request.query_params:
url += f"?{request.query_params}"
body = await request.body()
# Фильтруем заголовки, оставляем только нужные для API
allowed_headers = ['content-type', 'x-goog-api-key', 'authorization', 'x-goog-api-client']
headers = {k: v for k, v in request.headers.items() if k.lower() in allowed_headers}
try:
resp = await client.request(
method=request.method,
url=url,
content=body,
headers=headers,
timeout=60.0
)
return Response(
content=resp.content,
status_code=resp.status_code,
headers={"Content-Type": resp.headers.get("Content-Type", "application/json")}
)
except Exception as e:
return Response(content=str(e), status_code=500)
@app.get("/")
async def health():
return {"status": "proxy is running"}