from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.core.config import settings from app.db.database import Base, engine from app.models import models # noqa: F401 (register models) from app.services.storage import ensure_bucket from app.api.routes import health, collections, admin, auth @asynccontextmanager async def lifespan(app: FastAPI): Base.metadata.create_all(bind=engine) try: ensure_bucket() except Exception as e: # Don't crash the API if MinIO is briefly unavailable at startup. print(f"[startup] MinIO bucket init skipped: {e}") yield app = FastAPI( title=settings.PROJECT_NAME, version=settings.VERSION, lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=settings.ALLOWED_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(health.router, tags=["meta"]) app.include_router(auth.router, tags=["auth"]) app.include_router(collections.router, prefix="/collections", tags=["collections"]) app.include_router(admin.router, prefix="/admin", tags=["admin"])