Files
basicmachines-co-basic-memory/src/basic_memory/api/app.py
T
2025-01-22 14:32:41 -06:00

44 lines
1.2 KiB
Python

"""FastAPI application for basic-memory knowledge graph API."""
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.exception_handlers import http_exception_handler
from loguru import logger
from basic_memory import db
from .routers import knowledge, discovery, search, memory, resource
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifecycle manager for the FastAPI app."""
logger.info("Starting Basic Memory API")
yield
logger.info("Shutting down Basic Memory API")
await db.shutdown_db()
# Initialize FastAPI app
app = FastAPI(
title="Basic Memory API",
description="Knowledge graph API for basic-memory",
version="0.1.0",
lifespan=lifespan,
)
# Include routers
app.include_router(knowledge.router)
app.include_router(discovery.router)
app.include_router(search.router)
app.include_router(memory.router)
app.include_router(resource.router)
@app.exception_handler(Exception)
async def exception_handler(request, exc):
logger.exception(
f"An unhandled exception occurred for request '{request.url}', exception: {exc}"
)
return await http_exception_handler(request, HTTPException(status_code=500, detail=exc.args[0]))