vaka.pro commited on
Commit
8eabd7e
·
1 Parent(s): 61b054f

fix: reorders endpoints

Browse files
Files changed (1) hide show
  1. app.py +42 -41
app.py CHANGED
@@ -12,47 +12,6 @@ PUBLIC_ENDPOINT = os.getenv("PUBLIC_ENDPOINT")
12
  fastapi_app = FastAPI(title="Proxy Service")
13
 
14
 
15
- # Универсальный обработчик
16
- @fastapi_app.post("/{path:path}")
17
- async def proxy_webhook(path: str, request: Request):
18
- """Обработчик вебхуков с проверкой пути."""
19
- # Проверяем секретный путь
20
- if not PUBLIC_ENDPOINT or path != PUBLIC_ENDPOINT:
21
- raise HTTPException(status_code=404, detail="Not found")
22
-
23
- if not PRIVATE_SPACE_URL:
24
- raise HTTPException(status_code=500, detail="Service error")
25
-
26
- try:
27
- body_bytes = await request.body()
28
-
29
- # Минимальные заголовки
30
- headers = {
31
- "Content-Type": request.headers.get("content-type", "application/json"),
32
- }
33
-
34
- # Только заголовки
35
- incoming_headers = ["X-FORM-ID", "X-DELIVERY-ID", "X-FORM-ANSWER-ID"]
36
- for header in incoming_headers:
37
- if value := request.headers.get(header):
38
- headers[header] = value
39
-
40
- # Отправляем запрос
41
- response = requests.post(
42
- PRIVATE_SPACE_URL, data=body_bytes, headers=headers, timeout=25
43
- )
44
-
45
- # Возвращаем ответ
46
- return JSONResponse(
47
- status_code=response.status_code, content={"status": "processed"}
48
- )
49
-
50
- except requests.exceptions.Timeout:
51
- raise HTTPException(status_code=504, detail="Service timeout")
52
- except Exception:
53
- raise HTTPException(status_code=500, detail="Internal error")
54
-
55
-
56
  # Health check
57
  @fastapi_app.get("/health")
58
  async def health_check():
@@ -125,6 +84,48 @@ demo = create_gradio_interface()
125
  # КЛЮЧЕВОЕ ИЗМЕНЕНИЕ: монтируем Gradio на /ui, а не на /
126
  app = gr.mount_gradio_app(fastapi_app, demo, path="/ui")
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  # Локальный запуск
129
  if __name__ == "__main__":
130
  import uvicorn
 
12
  fastapi_app = FastAPI(title="Proxy Service")
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Health check
16
  @fastapi_app.get("/health")
17
  async def health_check():
 
84
  # КЛЮЧЕВОЕ ИЗМЕНЕНИЕ: монтируем Gradio на /ui, а не на /
85
  app = gr.mount_gradio_app(fastapi_app, demo, path="/ui")
86
 
87
+
88
+ # Универсальный обработчик
89
+ @fastapi_app.post("/{path:path}")
90
+ async def proxy_webhook(path: str, request: Request):
91
+ """Обработчик вебхуков с проверкой пути."""
92
+ # Проверяем секретный путь
93
+ if not PUBLIC_ENDPOINT or path != PUBLIC_ENDPOINT:
94
+ raise HTTPException(status_code=404, detail="Not found")
95
+
96
+ if not PRIVATE_SPACE_URL:
97
+ raise HTTPException(status_code=500, detail="Service error")
98
+
99
+ try:
100
+ body_bytes = await request.body()
101
+
102
+ # Минимальные заголовки
103
+ headers = {
104
+ "Content-Type": request.headers.get("content-type", "application/json"),
105
+ }
106
+
107
+ # Только заголовки
108
+ incoming_headers = ["X-FORM-ID", "X-DELIVERY-ID", "X-FORM-ANSWER-ID"]
109
+ for header in incoming_headers:
110
+ if value := request.headers.get(header):
111
+ headers[header] = value
112
+
113
+ # Отправляем запрос
114
+ response = requests.post(
115
+ PRIVATE_SPACE_URL, data=body_bytes, headers=headers, timeout=25
116
+ )
117
+
118
+ # Возвращаем ответ
119
+ return JSONResponse(
120
+ status_code=response.status_code, content={"status": "processed"}
121
+ )
122
+
123
+ except requests.exceptions.Timeout:
124
+ raise HTTPException(status_code=504, detail="Service timeout")
125
+ except Exception:
126
+ raise HTTPException(status_code=500, detail="Internal error")
127
+
128
+
129
  # Локальный запуск
130
  if __name__ == "__main__":
131
  import uvicorn