|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Q-Mem Bubble.io Gateway - Groq JTV Edition |
| 3 | +Q-Mem Bubble.io Gateway - Groq JTV Edition (Middleware Fix) |
4 | 4 | ========================================== |
5 | 5 | Secure HTTP gateway with benchmark endpoints: |
6 | 6 | - /api/bench/live - Live metrics JSON |
|
19 | 19 | from fastapi import FastAPI, HTTPException, Security, status, Depends, Response |
20 | 20 | from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials |
21 | 21 | from fastapi.middleware.cors import CORSMiddleware |
22 | | -from fastapi.responses import StreamingResponse, JSONResponse |
| 22 | +from fastapi.responses import StreamingResponse, JSONResponse, FileResponse, HTMLResponse |
23 | 23 | from pydantic import BaseModel |
24 | 24 | from typing import Optional, Dict, List |
25 | 25 | import numpy as np |
@@ -114,18 +114,13 @@ class PredictResponse(BaseModel): |
114 | 114 | version="2.0.0-groq-jtv" |
115 | 115 | ) |
116 | 116 |
|
117 | | -# CORS middleware |
118 | | -app.add_middleware( |
119 | | - CORSMiddleware, |
120 | | - allow_origins=["*"], # Configure appropriately for production |
121 | | - allow_credentials=True, |
122 | | - allow_methods=["*"], |
123 | | - allow_headers=["*"], |
124 | | -) |
125 | | - |
126 | 117 | # Startup time |
127 | 118 | GATEWAY_START_TIME = datetime.now() |
128 | 119 |
|
| 120 | +# NOTE: CORS middleware disabled due to FastAPI 0.101.0 / Starlette 0.52.1 incompatibility |
| 121 | +# CORS headers added manually to responses where needed |
| 122 | + |
| 123 | + |
129 | 124 |
|
130 | 125 | def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)): |
131 | 126 | """Verify bearer token""" |
@@ -522,6 +517,43 @@ async def recall_memory( |
522 | 517 | ) |
523 | 518 |
|
524 | 519 |
|
| 520 | +@app.get("/benchmark.html", tags=["static"]) |
| 521 | +@app.head("/benchmark.html", tags=["static"]) |
| 522 | +async def serve_benchmark(): |
| 523 | + """Serve benchmark.html page""" |
| 524 | + html_path = os.path.join(os.path.dirname(__file__), "benchmark.html") |
| 525 | + if os.path.exists(html_path): |
| 526 | + return FileResponse(html_path, media_type="text/html") |
| 527 | + raise HTTPException(status_code=404, detail="benchmark.html not found") |
| 528 | + |
| 529 | + |
| 530 | +@app.get("/telemetry_results.json", tags=["static"]) |
| 531 | +@app.head("/telemetry_results.json", tags=["static"]) |
| 532 | +async def serve_telemetry(): |
| 533 | + """Serve telemetry analysis results""" |
| 534 | + json_path = os.path.join(os.path.dirname(__file__), "telemetry_results.json") |
| 535 | + if os.path.exists(json_path): |
| 536 | + return FileResponse(json_path, media_type="application/json") |
| 537 | + raise HTTPException(status_code=404, detail="telemetry_results.json not found") |
| 538 | + |
| 539 | + |
| 540 | +@app.get("/", tags=["static"]) |
| 541 | +@app.head("/", tags=["static"]) |
| 542 | +async def serve_root(): |
| 543 | + """Redirect root to benchmark page""" |
| 544 | + html_path = os.path.join(os.path.dirname(__file__), "benchmark.html") |
| 545 | + if os.path.exists(html_path): |
| 546 | + return FileResponse(html_path, media_type="text/html") |
| 547 | + return JSONResponse( |
| 548 | + status_code=200, |
| 549 | + content={ |
| 550 | + "status": "online", |
| 551 | + "message": "Q-Mem Gateway", |
| 552 | + "endpoints": ["/api/bench/live", "/api/health", "/benchmark.html"] |
| 553 | + } |
| 554 | + ) |
| 555 | + |
| 556 | + |
525 | 557 | def main(): |
526 | 558 | parser = argparse.ArgumentParser(description='Q-Mem Bubble.io Gateway') |
527 | 559 | parser.add_argument('--host', default='0.0.0.0', help='Bind host') |
|
0 commit comments