from fastapi import FastAPI, Request from fastapi.staticfiles import StaticFiles from starlette.middleware.base import BaseHTTPMiddleware import uvicorn import os from pathlib import Path # Import PDF router and Visual HTML Editor router from html_to_pdf_router import router as pdf_router from visual_html_editor_router import router as editor_router # Ensure we're serving from the /workspace directory workspace_dir = "/workspace" class WorkspaceDirMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): # Check if workspace directory exists and recreate if deleted if not os.path.exists(workspace_dir): print(f"Workspace directory {workspace_dir} not found, recreating...") os.makedirs(workspace_dir, exist_ok=True) return await call_next(request) app = FastAPI() app.add_middleware(WorkspaceDirMiddleware) # Include routers app.include_router(pdf_router) app.include_router(editor_router) # Create output directory for generated PDFs (needed by PDF router) output_dir = Path("generated_pdfs") output_dir.mkdir(exist_ok=True) # Mount static files for PDF downloads app.mount("/downloads", StaticFiles(directory=str(output_dir)), name="downloads") # Initial directory creation os.makedirs(workspace_dir, exist_ok=True) # Add visual HTML editor root endpoint @app.get("/editor") async def list_html_files(): """List all HTML files in the workspace for easy access""" from fastapi.responses import HTMLResponse try: html_files = [f for f in os.listdir(workspace_dir) if f.endswith('.html')] html_content = """
Click-to-edit any HTML file with live preview
Add .html files to this directory to start editing