#!/usr/bin/env python3 """ NFOGuard Web Interface Starter Simple script to start web interface using existing config system """ import os import sys import time import uvicorn from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse # Import existing configuration (keep using core config for simplicity) from config.settings import config # Import existing database and components from core.database import NFOGuardDatabase # Add web interface path for routes only web_path = os.path.join(os.path.dirname(__file__), "nfoguard-web") if web_path not in sys.path: sys.path.append(web_path) # Import web routes from separated system try: from api.web_routes import register_web_routes as register_separated_web_routes use_separated_routes = True print("āœ… Using separated web routes with DELETE /api/episodes/ support") except ImportError: # Fallback to old routes if separated routes not available from api.web_routes import register_web_routes use_separated_routes = False print("āš ļø Using legacy web routes - DELETE functionality may be limited") # Import authentication system from api.auth import SimpleAuthMiddleware, AuthSession def create_web_app() -> FastAPI: """Create FastAPI web application""" app = FastAPI( title="NFOGuard Web Interface", description="Web interface for NFOGuard media database management", version="2.7.0-web", docs_url=None, # Disable docs in production redoc_url=None ) return app def setup_static_files(app: FastAPI) -> None: """Mount static file directories""" static_path = os.path.join(os.path.dirname(__file__), "nfoguard-web", "static") logo_path = os.path.join(os.path.dirname(__file__), "logo") print(f"šŸ” Checking static path: {static_path} (exists: {os.path.exists(static_path)})") print(f"šŸ” Checking logo path: {logo_path} (exists: {os.path.exists(logo_path)})") if os.path.exists(static_path): app.mount("/static", StaticFiles(directory=static_path), name="static") print(f"āœ… Mounted static files from: {static_path}") else: print(f"āŒ Static path not found: {static_path}") if os.path.exists(logo_path): app.mount("/logo", StaticFiles(directory=logo_path), name="logo") print(f"āœ… Mounted logo files from: {logo_path}") else: print(f"āŒ Logo path not found: {logo_path}") # Serve index.html at root @app.get("/") async def serve_index(): index_file = os.path.join(static_path, "index.html") if os.path.exists(index_file): return FileResponse(index_file) else: return {"message": "NFOGuard Web Interface", "status": "running"} # Serve favicon @app.get("/favicon.ico") async def serve_favicon(): # Try to serve favicon from logo directory or static files favicon_paths = [ os.path.join(logo_path, "favicon.ico"), os.path.join(static_path, "favicon.ico"), os.path.join(logo_path, "NFOGuardLogo.png") # Fallback to new logo ] for favicon_path in favicon_paths: if os.path.exists(favicon_path): return FileResponse(favicon_path) # Return 204 No Content if no favicon found from fastapi import Response return Response(status_code=204) # Health check endpoint for Docker @app.get("/health") async def health_check(): """Health check endpoint for Docker container monitoring""" try: # Basic health check - verify the web service is responsive return { "status": "healthy", "service": "nfoguard-web", "timestamp": time.time(), "version": "2.8.0-web" } except Exception as e: from fastapi import HTTPException raise HTTPException(status_code=503, detail=f"Health check failed: {e}") def main(): """Main entry point for NFOGuard Web Interface""" print("🌐 Starting NFOGuard Web Interface...") # Use existing config system web_host = os.environ.get("WEB_HOST", "0.0.0.0") web_port = int(os.environ.get("WEB_PORT", "8081")) print(f"šŸ“Š Configuration: Port {web_port}") # Create FastAPI app app = create_web_app() # Initialize database using existing system try: db = NFOGuardDatabase(config) print(f"āœ… Connected to database: {config.db_host}:{config.db_port}/{config.db_name}") except Exception as e: print(f"āŒ Failed to connect to database: {e}") sys.exit(1) # Setup authentication if enabled auth_enabled = getattr(config, 'web_auth_enabled', False) session_manager = None if auth_enabled: session_timeout = getattr(config, 'web_auth_session_timeout', 3600) session_manager = AuthSession(timeout_seconds=session_timeout) print(f"šŸ” Web authentication enabled (session timeout: {session_timeout}s)") else: print("🌐 Web authentication disabled") # Create dependencies for dependency injection dependencies = { "db": db, "config": config, "nfo_manager": None, # Not needed for read-only web interface "movie_processor": None, # Not needed for read-only web interface "tv_processor": None, # Not needed for read-only web interface "auth_enabled": auth_enabled, "session_manager": session_manager } # Add authentication middleware if enabled (BEFORE routes) if auth_enabled: app.add_middleware(SimpleAuthMiddleware, config=config, session_manager=session_manager) print("šŸ” Authentication middleware added to web interface") # Setup static files and routes setup_static_files(app) # Register web routes (prefer separated routes if available) if use_separated_routes: register_separated_web_routes(app, dependencies) print("āœ… Registered separated web routes with full /api/episodes/ support") else: register_web_routes(app, dependencies) print("āš ļø Using legacy web routes") print(f"šŸš€ Starting web server on {web_host}:{web_port}") try: uvicorn.run( app, host=web_host, port=web_port, workers=1, log_level="info", access_log=False ) except KeyboardInterrupt: print("\nšŸ›‘ Web interface shutdown by user") except Exception as e: print(f"āŒ Web interface failed: {e}") sys.exit(1) if __name__ == "__main__": main()