Fix frontend serving in main.py
Browse files- backend/main.py +53 -7
backend/main.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
|
| 4 |
Main entry point for the backend server.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import logging
|
|
|
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
|
| 10 |
-
from fastapi import FastAPI
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 13 |
|
| 14 |
from .config import get_settings
|
| 15 |
from .routes import router
|
|
@@ -26,23 +28,27 @@ logger = logging.getLogger(__name__)
|
|
| 26 |
@asynccontextmanager
|
| 27 |
async def lifespan(app: FastAPI):
|
| 28 |
"""Startup and shutdown events."""
|
| 29 |
-
|
| 30 |
-
logger.info("Starting LocalShortsLab...")
|
| 31 |
await init_job_manager()
|
| 32 |
|
| 33 |
yield
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Create FastAPI app
|
| 36 |
app = FastAPI(
|
| 37 |
title="Rovin API",
|
| 38 |
-
description="
|
| 39 |
version="1.0.0",
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
# CORS middleware
|
| 43 |
app.add_middleware(
|
| 44 |
CORSMiddleware,
|
| 45 |
-
allow_origins=["*"],
|
| 46 |
allow_credentials=True,
|
| 47 |
allow_methods=["*"],
|
| 48 |
allow_headers=["*"],
|
|
@@ -51,6 +57,46 @@ app.add_middleware(
|
|
| 51 |
# Include API routes
|
| 52 |
app.include_router(router, prefix="/api")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
async def health():
|
| 55 |
"""Health check endpoint."""
|
| 56 |
return {"status": "healthy"}
|
|
|
|
| 1 |
"""
|
| 2 |
+
Rovin - FastAPI Application
|
| 3 |
|
| 4 |
Main entry point for the backend server.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import logging
|
| 8 |
+
from pathlib import Path
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
|
| 11 |
+
from fastapi import FastAPI, HTTPException
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from fastapi.staticfiles import StaticFiles
|
| 14 |
+
from fastapi.responses import FileResponse
|
| 15 |
|
| 16 |
from .config import get_settings
|
| 17 |
from .routes import router
|
|
|
|
| 28 |
@asynccontextmanager
|
| 29 |
async def lifespan(app: FastAPI):
|
| 30 |
"""Startup and shutdown events."""
|
| 31 |
+
logger.info("Starting Rovin...")
|
|
|
|
| 32 |
await init_job_manager()
|
| 33 |
|
| 34 |
yield
|
| 35 |
|
| 36 |
+
logger.info("Shutting down Rovin...")
|
| 37 |
+
await shutdown_job_manager()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
# Create FastAPI app
|
| 41 |
app = FastAPI(
|
| 42 |
title="Rovin API",
|
| 43 |
+
description="AI-powered Short Video Generator",
|
| 44 |
version="1.0.0",
|
| 45 |
+
lifespan=lifespan,
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# CORS middleware
|
| 49 |
app.add_middleware(
|
| 50 |
CORSMiddleware,
|
| 51 |
+
allow_origins=["*"],
|
| 52 |
allow_credentials=True,
|
| 53 |
allow_methods=["*"],
|
| 54 |
allow_headers=["*"],
|
|
|
|
| 57 |
# Include API routes
|
| 58 |
app.include_router(router, prefix="/api")
|
| 59 |
|
| 60 |
+
# Serve Frontend Static Files (Production)
|
| 61 |
+
# Check for frontend build directory
|
| 62 |
+
static_dir = Path(__file__).parent.parent / "frontend" / "dist"
|
| 63 |
+
|
| 64 |
+
if static_dir.exists():
|
| 65 |
+
logger.info(f"Serving frontend from: {static_dir}")
|
| 66 |
+
|
| 67 |
+
# Mount assets directory
|
| 68 |
+
assets_dir = static_dir / "assets"
|
| 69 |
+
if assets_dir.exists():
|
| 70 |
+
app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")
|
| 71 |
+
|
| 72 |
+
# Serve index.html for root
|
| 73 |
+
@app.get("/")
|
| 74 |
+
async def serve_index():
|
| 75 |
+
return FileResponse(static_dir / "index.html")
|
| 76 |
+
|
| 77 |
+
# Catch-all route for SPA (must be last)
|
| 78 |
+
@app.get("/{full_path:path}")
|
| 79 |
+
async def serve_spa(full_path: str):
|
| 80 |
+
# Don't catch API routes
|
| 81 |
+
if full_path.startswith("api"):
|
| 82 |
+
raise HTTPException(status_code=404, detail="API endpoint not found")
|
| 83 |
+
|
| 84 |
+
# Check if it's a static file
|
| 85 |
+
file_path = static_dir / full_path
|
| 86 |
+
if file_path.exists() and file_path.is_file():
|
| 87 |
+
return FileResponse(file_path)
|
| 88 |
+
|
| 89 |
+
# Otherwise return index.html for client-side routing
|
| 90 |
+
return FileResponse(static_dir / "index.html")
|
| 91 |
+
else:
|
| 92 |
+
logger.warning(f"Frontend not found at {static_dir}. Running API only mode.")
|
| 93 |
+
|
| 94 |
+
@app.get("/")
|
| 95 |
+
async def root():
|
| 96 |
+
return {"message": "Rovin API is running", "status": "healthy"}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@app.get("/health")
|
| 100 |
async def health():
|
| 101 |
"""Health check endpoint."""
|
| 102 |
return {"status": "healthy"}
|