from contextlib import asynccontextmanager import os from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse from backend.database import engine from backend.routers import health, subreddits, posts, comments, authors, analytics, digests, summaries @asynccontextmanager async def lifespan(app: FastAPI): yield await engine.dispose() app = FastAPI(title="Reddit Monitor", lifespan=lifespan) # API routes app.include_router(health.router, prefix="/api/v1") app.include_router(subreddits.router, prefix="/api/v1") app.include_router(posts.router, prefix="/api/v1") app.include_router(comments.router, prefix="/api/v1") app.include_router(authors.router, prefix="/api/v1") app.include_router(analytics.router, prefix="/api/v1") app.include_router(digests.router, prefix="/api/v1") app.include_router(summaries.router, prefix="/api/v1") # SPA static file serving (only when frontend is built) STATIC_DIR = "/app/static" _index_html = "" if os.path.isdir(STATIC_DIR): with open(os.path.join(STATIC_DIR, "index.html")) as f: _index_html = f.read() app.mount("/assets", StaticFiles(directory=os.path.join(STATIC_DIR, "assets")), name="assets") @app.get("/{full_path:path}") async def serve_spa(full_path: str): return HTMLResponse(_index_html)