AgentizedSolutions commited on
Commit
f5f5b59
·
verified ·
1 Parent(s): 4bfc335

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -0
main.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Request, Response, HTTPException
3
+ import httpx
4
+
5
+ app = FastAPI()
6
+
7
+ # Читаємо налаштування з Secrets
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+ TARGET_SPACE_URL = os.getenv("TARGET_SPACE_URL")
10
+
11
+ @app.get("/")
12
+ async def root():
13
+ # Фейкова сторінка-заглушка. Будь-який лівий юзер побачить це і піде геть
14
+ return {"status": "error", "message": "404 Not Found - Service Under Construction"}
15
+
16
+ @app.post("/listener")
17
+ async def listen_webhook(request: Request):
18
+ if not TARGET_SPACE_URL or not HF_TOKEN:
19
+ raise HTTPException(status_code=500, detail="Bridge configuration missing.")
20
+
21
+ # Забираємо JSON, який прислав поштовий сервіс
22
+ payload = await request.json()
23
+
24
+ # Формуємо заголовки для пробиття приватності твого головного Space
25
+ headers = {
26
+ "Authorization": f"Bearer {HF_TOKEN}",
27
+ "Content-Type": "application/json"
28
+ }
29
+
30
+ # Перенаправляємо запит у твій приватний Space
31
+ async with httpx.AsyncClient() as client:
32
+ try:
33
+ response = await client.post(TARGET_SPACE_URL, json=payload, headers=headers, timeout=10.0)
34
+ # Повертаємо поштовому сервісу те, що відповів твій приватний агент (наприклад, 200 OK)
35
+ return Response(content=response.content, status_code=response.status_code, media_type="application/json")
36
+ except httpx.RequestError as exc:
37
+ return {"status": "failed", "error": f"Could not forward webhook: {exc}"}