""" Embedded server that serves React client and OCR API """ import os import sys from pathlib import Path from fastapi import FastAPI, Request from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse import uvicorn # Import the main app and its routes from app import app as ocr_app # Create main FastAPI app main_app = FastAPI(title="SPaCial AI OCR - Portable Edition") # Include all OCR API routes directly (not mounted under /api) # This makes the API available at /ocr/process, /export/pdf, etc. main_app.include_router(ocr_app.router) # Serve React client static files static_path = Path(__file__).parent / "static" if static_path.exists(): # Mount assets directory for JS/CSS files assets_path = static_path / "assets" if assets_path.exists(): main_app.mount("/assets", StaticFiles(directory=str(assets_path)), name="assets") @main_app.get("/vite.svg") async def serve_vite_svg(): """Serve vite.svg""" svg_file = Path(__file__).parent / "static" / "vite.svg" if svg_file.exists(): return FileResponse(svg_file) return FileResponse(Path(__file__).parent / "static" / "index.html") @main_app.get("/") async def serve_client(): """Serve the React client""" client_file = Path(__file__).parent / "static" / "index.html" if client_file.exists(): return FileResponse(client_file) return {"message": "SPaCial AI OCR Server is running", "docs": "/docs"} @main_app.get("/health") async def health_check(): """Health check endpoint""" return {"status": "healthy", "version": "1.0.0"} if __name__ == "__main__": # Get port from environment or use default port = int(os.environ.get("PORT", 8000)) host = os.environ.get("HOST", "127.0.0.1") print(f"Starting SPaCial AI OCR Server...") print(f"Server: http://{host}:{port}") print(f"Client: http://{host}:{port}") print(f"API Docs: http://{host}:{port}/docs") print("\nPress Ctrl+C to stop") uvicorn.run(main_app, host=host, port=port, log_level="info")