#!/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 # Add web interface path for imports sys.path.append(os.path.join(os.path.dirname(__file__), "nfoguard-web")) # Import web-specific components from config.web_settings import web_config as config from core.web_database import WebDatabase from api.web_routes import register_web_routes from api.auth import SimpleAuthMiddleware, create_auth_dependencies 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 web config system web_host = config.web_host web_port = config.web_port print(f"šŸ“Š Configuration: Port {web_port}") # Create FastAPI app app = create_web_app() # Initialize web database (read-only optimized) try: db = WebDatabase( db_type=config.db_type, host=config.db_host, port=config.db_port, database=config.db_name, user=config.db_user, password=config.db_password ) 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) # Create dependencies for dependency injection dependencies = { "db": db, "config": config } # Add authentication dependencies if enabled if config.web_auth_enabled: auth_deps = create_auth_dependencies(config) dependencies.update(auth_deps) # Add authentication middleware app.add_middleware(SimpleAuthMiddleware, config=config) print(f"šŸ” Web authentication enabled for user: {config.web_auth_username}") else: print("šŸ”“ Web authentication disabled - interface is public") # Setup static files and routes setup_static_files(app) # Register web routes register_web_routes(app, dependencies) 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()