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 = """ Web Demo

Welcome to Web Demo Application

Application Information

Service: Web Frontend

Status: ✓ Running

Version: 1.0.0

Metrics Dashboard

Requests: 1,234 Uptime: 99.9% Users: 567

About

This is a demo FastAPI web application serving HTML content. It demonstrates a simple web interface with metrics and information display.

📚 View Documentation 🏥 Health Check
""" @app.get("/", response_class=HTMLResponse) async def root(): """Serve the main webpage""" return HTML_TEMPLATE @app.get("/health") async def health(): """Health check endpoint""" return {"status": "healthy", "service": "web"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)