from fastapi import FastAPI from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles import uvicorn import os import subprocess app = FastAPI(title="Web Demo Application") # Build MkDocs documentation on startup def build_docs(): docs_dir = os.path.join(os.path.dirname(__file__), "docs") site_dir = os.path.join(os.path.dirname(__file__), "site") if os.path.exists(docs_dir): try: subprocess.run( ["mkdocs", "build", "-f", os.path.join(docs_dir, "mkdocs.yml"), "-d", site_dir], check=True, capture_output=True ) print(f"✓ Documentation built successfully at {site_dir}") return True except subprocess.CalledProcessError as e: print(f"✗ Failed to build documentation: {e.stderr.decode()}") return False except FileNotFoundError: print("✗ MkDocs not installed. Install with: pip install mkdocs mkdocs-material") return False return False # Build docs on startup @app.on_event("startup") async def startup_event(): build_docs() # Mount static documentation site at /docs site_dir = os.path.join(os.path.dirname(__file__), "site") if os.path.exists(site_dir): app.mount("/docs", StaticFiles(directory=site_dir, html=True), name="docs") # Simple HTML template inline HTML_TEMPLATE = """
Service: Web Frontend
Status: ✓ Running
Version: 1.0.0
This is a demo FastAPI web application serving HTML content. It demonstrates a simple web interface with metrics and information display.