Files
document-service/app/main.py
HaimKortovich ff3d93e933
All checks were successful
Build and Publish / build-release (push) Successful in 44s
add bucket in CRD
2026-04-24 14:05:28 -05:00

82 lines
2.0 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from app.routers import documents
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="/api/openapi",
docs_url="/api/docs",
redoc_url="/api/redoc"
)
# Add auth middleware
app.add_middleware(AuthMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
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