2026-02-09 15:49:50 +05:30
|
|
|
"""FastAPI application entrypoint and router wiring for the backend."""
|
|
|
|
|
|
2026-02-01 22:25:28 +05:30
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-06 11:57:29 +05:30
|
|
|
from contextlib import asynccontextmanager
|
2026-02-09 15:49:50 +05:30
|
|
|
from typing import TYPE_CHECKING
|
2026-02-06 11:57:29 +05:30
|
|
|
|
2026-02-04 02:28:51 +05:30
|
|
|
from fastapi import APIRouter, FastAPI
|
2026-02-01 22:25:28 +05:30
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2026-02-06 19:11:11 +05:30
|
|
|
from fastapi_pagination import add_pagination
|
2026-02-01 22:25:28 +05:30
|
|
|
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.api.activity import router as activity_router
|
2026-02-05 19:06:32 +05:30
|
|
|
from app.api.agent import router as agent_router
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.api.agents import router as agents_router
|
2026-02-05 14:43:25 +05:30
|
|
|
from app.api.approvals import router as approvals_router
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.api.auth import router as auth_router
|
2026-02-07 20:29:50 +05:30
|
|
|
from app.api.board_group_memory import router as board_group_memory_router
|
|
|
|
|
from app.api.board_groups import router as board_groups_router
|
2026-02-05 14:43:25 +05:30
|
|
|
from app.api.board_memory import router as board_memory_router
|
|
|
|
|
from app.api.board_onboarding import router as board_onboarding_router
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.api.boards import router as boards_router
|
|
|
|
|
from app.api.gateway import router as gateway_router
|
2026-02-04 23:07:22 +05:30
|
|
|
from app.api.gateways import router as gateways_router
|
2026-02-04 20:49:25 +05:30
|
|
|
from app.api.metrics import router as metrics_router
|
2026-02-08 21:16:26 +05:30
|
|
|
from app.api.organizations import router as organizations_router
|
2026-02-08 00:46:10 +05:30
|
|
|
from app.api.souls_directory import router as souls_directory_router
|
2026-02-12 18:35:48 +05:30
|
|
|
from app.api.tags import router as tags_router
|
2026-02-04 02:28:51 +05:30
|
|
|
from app.api.tasks import router as tasks_router
|
2026-02-04 20:21:33 +05:30
|
|
|
from app.api.users import router as users_router
|
2026-02-01 22:25:28 +05:30
|
|
|
from app.core.config import settings
|
2026-02-07 03:14:30 +05:30
|
|
|
from app.core.error_handling import install_error_handling
|
2026-02-11 16:49:43 +05:30
|
|
|
from app.core.logging import configure_logging, get_logger
|
2026-02-01 23:16:56 +05:30
|
|
|
from app.db.session import init_db
|
2026-02-01 22:25:28 +05:30
|
|
|
|
2026-02-09 15:49:50 +05:30
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import AsyncIterator
|
|
|
|
|
|
2026-02-02 20:51:08 +05:30
|
|
|
configure_logging()
|
2026-02-11 16:49:43 +05:30
|
|
|
logger = get_logger(__name__)
|
2026-02-02 20:51:08 +05:30
|
|
|
|
2026-02-06 11:57:29 +05:30
|
|
|
|
|
|
|
|
@asynccontextmanager
|
2026-02-06 16:12:04 +05:30
|
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
2026-02-09 15:49:50 +05:30
|
|
|
"""Initialize application resources before serving requests."""
|
2026-02-11 16:49:43 +05:30
|
|
|
logger.info(
|
|
|
|
|
"app.lifecycle.starting environment=%s db_auto_migrate=%s",
|
|
|
|
|
settings.environment,
|
|
|
|
|
settings.db_auto_migrate,
|
|
|
|
|
)
|
2026-02-06 16:12:04 +05:30
|
|
|
await init_db()
|
2026-02-11 16:49:43 +05:30
|
|
|
logger.info("app.lifecycle.started")
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
logger.info("app.lifecycle.stopped")
|
2026-02-06 11:57:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(title="Mission Control API", version="0.1.0", lifespan=lifespan)
|
2026-02-01 22:25:28 +05:30
|
|
|
|
|
|
|
|
origins = [o.strip() for o in settings.cors_origins.split(",") if o.strip()]
|
|
|
|
|
if origins:
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_credentials=True,
|
2026-02-01 23:16:56 +05:30
|
|
|
allow_methods=["*"],
|
2026-02-01 22:25:28 +05:30
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
2026-02-11 16:49:43 +05:30
|
|
|
logger.info("app.cors.enabled origins_count=%s", len(origins))
|
|
|
|
|
else:
|
|
|
|
|
logger.info("app.cors.disabled")
|
2026-02-01 22:25:28 +05:30
|
|
|
|
2026-02-07 03:14:30 +05:30
|
|
|
install_error_handling(app)
|
|
|
|
|
|
2026-02-01 23:16:56 +05:30
|
|
|
|
2026-02-01 22:25:28 +05:30
|
|
|
@app.get("/health")
|
2026-02-04 02:28:51 +05:30
|
|
|
def health() -> dict[str, bool]:
|
2026-02-09 15:49:50 +05:30
|
|
|
"""Lightweight liveness probe endpoint."""
|
2026-02-01 22:25:28 +05:30
|
|
|
return {"ok": True}
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
|
2026-02-04 20:21:33 +05:30
|
|
|
@app.get("/healthz")
|
|
|
|
|
def healthz() -> dict[str, bool]:
|
2026-02-09 15:49:50 +05:30
|
|
|
"""Alias liveness probe endpoint for platform compatibility."""
|
2026-02-04 20:21:33 +05:30
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/readyz")
|
|
|
|
|
def readyz() -> dict[str, bool]:
|
2026-02-09 15:49:50 +05:30
|
|
|
"""Readiness probe endpoint for service orchestration checks."""
|
2026-02-04 20:21:33 +05:30
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 02:28:51 +05:30
|
|
|
api_v1 = APIRouter(prefix="/api/v1")
|
|
|
|
|
api_v1.include_router(auth_router)
|
2026-02-05 19:06:32 +05:30
|
|
|
api_v1.include_router(agent_router)
|
2026-02-04 02:28:51 +05:30
|
|
|
api_v1.include_router(agents_router)
|
|
|
|
|
api_v1.include_router(activity_router)
|
|
|
|
|
api_v1.include_router(gateway_router)
|
2026-02-04 23:07:22 +05:30
|
|
|
api_v1.include_router(gateways_router)
|
2026-02-04 20:49:25 +05:30
|
|
|
api_v1.include_router(metrics_router)
|
2026-02-08 21:16:26 +05:30
|
|
|
api_v1.include_router(organizations_router)
|
2026-02-08 00:46:10 +05:30
|
|
|
api_v1.include_router(souls_directory_router)
|
2026-02-07 20:29:50 +05:30
|
|
|
api_v1.include_router(board_groups_router)
|
|
|
|
|
api_v1.include_router(board_group_memory_router)
|
2026-02-04 02:28:51 +05:30
|
|
|
api_v1.include_router(boards_router)
|
2026-02-05 14:43:25 +05:30
|
|
|
api_v1.include_router(board_memory_router)
|
|
|
|
|
api_v1.include_router(board_onboarding_router)
|
|
|
|
|
api_v1.include_router(approvals_router)
|
2026-02-04 02:28:51 +05:30
|
|
|
api_v1.include_router(tasks_router)
|
2026-02-12 18:35:48 +05:30
|
|
|
api_v1.include_router(tags_router)
|
2026-02-04 20:21:33 +05:30
|
|
|
api_v1.include_router(users_router)
|
2026-02-04 02:28:51 +05:30
|
|
|
app.include_router(api_v1)
|
2026-02-06 19:11:11 +05:30
|
|
|
|
|
|
|
|
add_pagination(app)
|
2026-02-11 16:49:43 +05:30
|
|
|
logger.debug("app.routes.registered count=%s", len(app.routes))
|