Spaces:
Building
Building
from fastapi import FastAPI | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.responses import FileResponse | |
import uvicorn | |
import os | |
from pathlib import Path | |
from utils import log | |
from chat_handler import router as chat_router # β start_session & chat | |
from admin_routes import router as admin_router # β Admin API endpoints | |
from spark_startup import run_in_thread | |
app = FastAPI( | |
title="Flare Orchestration Service", | |
version="0.1.0", | |
description="LLM-driven intent & API flow engine (bootstrap)", | |
) | |
run_in_thread() | |
# ---------------- Core chat/session routes -------------------------- | |
app.include_router(chat_router) | |
# ---------------- Admin API routes ---------------------------------- | |
app.include_router(admin_router) | |
# ---------------- Health probe (HF Spaces watchdog) ----------------- | |
def health_check(): | |
return {"status": "ok"} | |
# ---------------- Serve Angular UI if exists ------------------------ | |
static_path = Path("static") | |
log(f"π Checking for static directory at: {static_path.absolute()}") | |
log(f"π Static directory exists: {static_path.exists()}") | |
if static_path.exists() and static_path.is_dir(): | |
# List files in static directory | |
files = list(static_path.iterdir()) | |
log(f"π Files in static directory: {[f.name for f in files]}") | |
# Check for index.html | |
index_path = static_path / "index.html" | |
log(f"π index.html exists: {index_path.exists()}") | |
# Serve static files (Angular assets) - only if assets directory exists | |
assets_path = static_path / "assets" | |
if assets_path.exists() and assets_path.is_dir(): | |
app.mount("/assets", StaticFiles(directory=str(assets_path)), name="assets") | |
# Root path - serve index.html | |
async def serve_root(): | |
index_path = static_path / "index.html" | |
if index_path.exists(): | |
log("π Serving index.html") | |
return FileResponse(str(index_path)) | |
log("β οΈ index.html not found, returning health check") | |
return {"status": "ok", "sessions": len(session_store._sessions)} # Fallback to health check | |
# Catch-all route for Angular routing (must be last!) | |
async def serve_angular(full_path: str): | |
# Don't catch API routes | |
if full_path.startswith("api/") or full_path in ["start_session", "chat", "health"]: | |
return {"error": "Not found"}, 404 | |
# Return Angular index.html for all other routes | |
index_path = static_path / "index.html" | |
if index_path.exists(): | |
return FileResponse(str(index_path)) | |
return {"error": "UI not found"}, 404 | |
else: | |
log("β οΈ Static directory not found") | |
# No UI built, just health endpoint | |
def health_check(): | |
return {"status": "ok", "message": "UI not found. Build Angular app first."}I not found. Build Angular app first."} | |
if __name__ == "__main__": | |
log("π Starting Flare backend β¦") | |
uvicorn.run(app, host="0.0.0.0", port=7860) |