Spaces:
Running
Running
Deploy 25df254e
Browse files- backend/api.py +28 -0
backend/api.py
CHANGED
|
@@ -36,6 +36,34 @@ from backend.integrations.endpoints import register_endpoint, unregister_endpoin
|
|
| 36 |
app = FastAPI(title="OpenCodeReview API", version="0.1.0")
|
| 37 |
logger = logging.getLogger(__name__)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
allowed_origins = [item.strip() for item in os.getenv("OCR_ALLOWED_ORIGINS", "http://localhost:5173").split(",") if item.strip()]
|
| 40 |
app.add_middleware(CORSMiddleware, allow_origins=allowed_origins, allow_methods=["GET", "POST"], allow_headers=["*"], allow_credentials=True)
|
| 41 |
|
|
|
|
| 36 |
app = FastAPI(title="OpenCodeReview API", version="0.1.0")
|
| 37 |
logger = logging.getLogger(__name__)
|
| 38 |
|
| 39 |
+
def _temporary_cloudflare_probe():
|
| 40 |
+
"""Compare small authenticated requests; never log credentials or content."""
|
| 41 |
+
import httpx
|
| 42 |
+
account = os.getenv("CLOUDFLARE_ACCOUNT_ID", "").strip()
|
| 43 |
+
token = os.getenv("CLOUDFLARE_API_TOKEN", "").strip()
|
| 44 |
+
if not account or not token:
|
| 45 |
+
logger.warning("Cloudflare probe: credentials missing")
|
| 46 |
+
return
|
| 47 |
+
url = f"https://api.cloudflare.com/client/v4/accounts/{account}/ai/v1/chat/completions"
|
| 48 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 49 |
+
body = {"model": "@cf/zai-org/glm-4.7-flash", "messages": [{"role": "user", "content": "Say hello."}], "max_tokens": 64}
|
| 50 |
+
for label, call in (("httpx", lambda: httpx.post(url, headers=headers, json=body, timeout=30)),
|
| 51 |
+
("requests", lambda: requests.post(url, headers=headers, json=body, timeout=30))):
|
| 52 |
+
start = time.monotonic()
|
| 53 |
+
try:
|
| 54 |
+
response = call()
|
| 55 |
+
logger.warning("Cloudflare probe: %s status=%s duration=%.2fs", label, response.status_code, time.monotonic() - start)
|
| 56 |
+
except Exception as exc:
|
| 57 |
+
types, seen = [], set()
|
| 58 |
+
while exc is not None and id(exc) not in seen and len(types) < 6:
|
| 59 |
+
seen.add(id(exc))
|
| 60 |
+
types.append(type(exc).__name__)
|
| 61 |
+
exc = exc.__cause__ or exc.__context__
|
| 62 |
+
logger.warning("Cloudflare probe: %s duration=%.2fs error types=%s", label, time.monotonic() - start, " -> ".join(types))
|
| 63 |
+
time.sleep(5)
|
| 64 |
+
|
| 65 |
+
threading.Thread(target=_temporary_cloudflare_probe, daemon=True).start()
|
| 66 |
+
|
| 67 |
allowed_origins = [item.strip() for item in os.getenv("OCR_ALLOWED_ORIGINS", "http://localhost:5173").split(",") if item.strip()]
|
| 68 |
app.add_middleware(CORSMiddleware, allow_origins=allowed_origins, allow_methods=["GET", "POST"], allow_headers=["*"], allow_credentials=True)
|
| 69 |
|