Sk0lovek commited on
Commit
4952bf4
·
verified ·
1 Parent(s): 3a0572a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -2,41 +2,37 @@ from fastapi import FastAPI, Request, Response
2
  import httpx
3
 
4
  app = FastAPI()
5
-
6
- # Базовый URL Google AI Studio
7
  GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
8
 
9
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
10
  async def proxy(path: str, request: Request):
11
  async with httpx.AsyncClient() as client:
12
- # Собираем URL
13
  url = f"{GOOGLE_API_URL}/{path}"
14
  if request.query_params:
15
  url += f"?{request.query_params}"
16
 
17
- # Читаем тело запроса
18
  body = await request.body()
19
 
20
- # Копируем заголовки (особенно важен Authorization или x-goog-api-key)
21
- headers = dict(request.headers)
22
- # Удаляем хост, чтобы Google не ругался
23
- headers.pop("host", None)
24
 
25
- # Делаем запрос к Google
26
- resp = await client.request(
27
- method=request.method,
28
- url=url,
29
- content=body,
30
- headers=headers,
31
- timeout=60.0
32
- )
33
-
34
- return Response(
35
- content=resp.content,
36
- status_code=resp.status_code,
37
- headers=dict(resp.headers)
38
- )
 
39
 
40
- if __name__ == "__main__":
41
- import uvicorn
42
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
2
  import httpx
3
 
4
  app = FastAPI()
 
 
5
  GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
6
 
7
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
8
  async def proxy(path: str, request: Request):
9
  async with httpx.AsyncClient() as client:
 
10
  url = f"{GOOGLE_API_URL}/{path}"
11
  if request.query_params:
12
  url += f"?{request.query_params}"
13
 
 
14
  body = await request.body()
15
 
16
+ # Фильтруем заголовки, оставляем только нужные для API
17
+ allowed_headers = ['content-type', 'x-goog-api-key', 'authorization', 'x-goog-api-client']
18
+ headers = {k: v for k, v in request.headers.items() if k.lower() in allowed_headers}
 
19
 
20
+ try:
21
+ resp = await client.request(
22
+ method=request.method,
23
+ url=url,
24
+ content=body,
25
+ headers=headers,
26
+ timeout=60.0
27
+ )
28
+ return Response(
29
+ content=resp.content,
30
+ status_code=resp.status_code,
31
+ headers={"Content-Type": resp.headers.get("Content-Type", "application/json")}
32
+ )
33
+ except Exception as e:
34
+ return Response(content=str(e), status_code=500)
35
 
36
+ @app.get("/")
37
+ async def health():
38
+ return {"status": "proxy is running"}