from pathlib import Path from fastapi import FastAPI, HTTPException, Request, status from fastapi.responses import FileResponse, JSONResponse from app import __version__ from app.api import auth, health from app.config import get_settings from app.services.users import UserError def _register_frontend(app: FastAPI, static_dir: Path) -> None: """Serve the built Vue SPA; unknown non-API paths fall back to index.html.""" root = static_dir.resolve() index_file = root / "index.html" @app.get("/{path:path}", include_in_schema=False) def spa(path: str) -> FileResponse: if path == "api" or path.startswith("api/") or not index_file.is_file(): raise HTTPException(status_code=404) candidate = (root / path).resolve() if path and candidate.is_relative_to(root) and candidate.is_file(): return FileResponse(candidate) return FileResponse(index_file, headers={"Cache-Control": "no-cache"}) def create_app() -> FastAPI: settings = get_settings() app = FastAPI( title="LAN-Clipboard", version=__version__, docs_url="/api/docs", openapi_url="/api/openapi.json", redoc_url=None, ) app.include_router(health.router, prefix="/api") app.include_router(auth.router, prefix="/api") @app.exception_handler(UserError) def user_error_handler(_request: Request, exc: UserError) -> JSONResponse: return JSONResponse({"detail": str(exc)}, status_code=status.HTTP_400_BAD_REQUEST) _register_frontend(app, settings.static_dir) return app app = create_app()