spore-commons/node/spore_node/api/main.py

52 lines
1.6 KiB
Python

"""FastAPI application factory for Spore Agent Commons.
This mounts our custom routers onto the koi-net node's FastAPI app.
If running standalone (without koi-net), creates its own app.
"""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from spore_node.config import SporeConfig
from spore_node.db.connection import init_pool, close_pool
from spore_node.api.routers import health, holons, governance, claims, intents, commitments
log = logging.getLogger(__name__)
def mount_routers(app: FastAPI) -> None:
"""Mount Spore-specific routers onto a FastAPI app."""
app.include_router(health.router)
app.include_router(holons.router)
app.include_router(governance.router)
app.include_router(claims.router)
app.include_router(intents.router)
app.include_router(commitments.router)
def create_standalone_app(cfg: SporeConfig | None = None) -> FastAPI:
"""Create a standalone FastAPI app (without koi-net node).
Used for development/testing or when koi-net is optional.
"""
if cfg is None:
cfg = SporeConfig()
@asynccontextmanager
async def lifespan(app: FastAPI):
log.info("Starting Spore Commons API...")
await init_pool(cfg.database_url)
yield
await close_pool()
log.info("Spore Commons API stopped.")
app = FastAPI(
title="Spore Agent Commons",
version="0.1.0",
description="Agent Commons — governance memory, knowledge graph, and federation layer",
lifespan=lifespan,
)
mount_routers(app)
return app