48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from contextlib import asynccontextmanager
|
|
from collections.abc import AsyncIterator
|
|
import logging
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from app.api import alerts, assets, auth, health, monitors, notifications
|
|
from app.core.config import settings
|
|
from app.db.session import SessionLocal
|
|
from app.services.bootstrap import ensure_initial_admin
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
try:
|
|
with SessionLocal() as db:
|
|
ensure_initial_admin(db)
|
|
except SQLAlchemyError as exc:
|
|
logger.warning("Initial admin bootstrap skipped because the database is unavailable: %s", exc)
|
|
yield
|
|
|
|
|
|
app = FastAPI(
|
|
title="InfraPulse API",
|
|
version="0.1.0",
|
|
description="Self-hosted infrastructure monitoring API",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(settings.frontend_url)],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(health.router)
|
|
app.include_router(auth.router)
|
|
app.include_router(assets.router)
|
|
app.include_router(monitors.router)
|
|
app.include_router(alerts.router)
|
|
app.include_router(notifications.router)
|