from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi from app.routers import documents from app.config import settings from app.logger import setup_logging from app.middleware.auth import AuthMiddleware # Setup logging setup_logging() app = FastAPI( title="Document Service", version="1.0.0", description="Generic document management service with S3 storage and PDF field discovery", openapi_url="/openapi", docs_url="/docs", redoc_url="/redoc" ) # Add auth middleware app.add_middleware(AuthMiddleware) app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:3000"], allow_methods=["*"], allow_headers=["*"] ) app.include_router(documents.router) @app.on_event("startup") async def startup_event(): """Run startup tasks. Raises: Exception: If S3 bucket initialization fails (service will fail to start) """ from app import s3 from app.logger import get_logger logger = get_logger(__name__) logger.info("Starting up document service...") try: s3.ensure_bucket_exists() logger.info("S3 bucket initialization complete") except Exception as e: logger.error(f"Failed to initialize S3 bucket: {e}") # Re-raise to fail startup raise @app.get("/health", tags=["health"]) def health(): return {"status": "ok"} @app.get("/health/ready", tags=["health"]) def health_ready(): """Health check for Kubernetes readiness probes.""" return {"status": "ready"} def custom_openapi(): if app.openapi_schema: return app.openapi_schema schema = get_openapi( title="Document Service", version="1.0.0", openapi_version="3.1.0", description="Generic document management service with S3 storage and PDF field discovery", routes=app.routes ) schema["servers"] = [ {"url": "http://localhost:8082", "description": "Local dev"} ] app.openapi_schema = schema return app.openapi_schema app.openapi = custom_openapi