Files
benchmark-showcase/main.py
2025-03-26 00:05:30 +03:00

56 lines
1.7 KiB
Python

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import os
import benchmarks
app = FastAPI(title="RAG Benchmark Dashboard")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, replace with specific origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# API routes
app.include_router(benchmarks.router, prefix="/api/benchmarks", tags=["benchmarks"])
# Define paths
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(ROOT_DIR, "static")
# Mount static files directory
if os.path.exists(STATIC_DIR):
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
# Place your new vanilla JS dashboard here
DASHBOARD_FILE = os.path.join(STATIC_DIR, "index.html")
@app.get("/", tags=["dashboard"])
async def serve_root_dashboard():
if os.path.exists(DASHBOARD_FILE):
return FileResponse(DASHBOARD_FILE)
else:
raise HTTPException(status_code=404, detail="Dashboard file not found")
@app.get("/{full_path:path}", tags=["dashboard"])
async def serve_dashboard_for_path(full_path: str):
# Don't catch API routes
if full_path.startswith("api/"):
raise HTTPException(status_code=404, detail="Not found")
# Don't catch static files
if full_path.startswith("static/"):
raise HTTPException(status_code=404, detail="Not found")
# Serve the same dashboard.html for any other path (SPA routing)
if os.path.exists(DASHBOARD_FILE):
return FileResponse(DASHBOARD_FILE)
else:
raise HTTPException(status_code=404, detail="Dashboard file not found")