Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /avinash-mall_CognitiveMemoryLayer /src /api /app.py
| """FastAPI application factory and entry point.""" | |
| import asyncio | |
| import pathlib | |
| from collections.abc import AsyncGenerator | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import FileResponse, JSONResponse, Response | |
| from fastapi.staticfiles import StaticFiles | |
| from sqlalchemy import text as sql_text | |
| from starlette.middleware.base import BaseHTTPMiddleware | |
| from ..core.config import get_settings, validate_embedding_dimensions | |
| from ..core.exceptions import ( | |
| CognitiveMemoryError, | |
| MemoryAccessDenied, | |
| MemoryNotFoundError, | |
| ) | |
| from ..core.exceptions import ( | |
| ValidationError as CMLValidationError, | |
| ) | |
| from ..storage.connection import DatabaseManager | |
| from .admin_routes import admin_router | |
| from .dashboard import dashboard_router | |
| from .middleware import RateLimitMiddleware, RequestLoggingMiddleware | |
| from .routes import router | |
| # Resolve dashboard static files directory | |
| _DASHBOARD_DIR = pathlib.Path(__file__).resolve().parent.parent / "dashboard" / "static" | |
| async def lifespan(app: FastAPI) -> AsyncGenerator: | |
| """Application lifespan handler.""" | |
| # Validate embedding dimensions match DB schema at startup | |
| validate_embedding_dimensions() | |
| db_manager = await DatabaseManager.create() | |
| app.state.db = db_manager | |
| # Pre-warm the PostgreSQL connection pool so early requests don't pay | |
| # the 150ms-per-connection cold-start cost under concurrent load. Capped at | |
| # this process's pool_size: a flat 20 here, times N workers, exceeded | |
| # postgres max_connections=100 and killed multi-worker startup. | |
| from ..storage.connection import pg_pool_sizes | |
| async def _pg_ping() -> None: | |
| async with db_manager.pg_session() as _s: | |
| await _s.execute(sql_text("SELECT 1")) | |
| _prewarm = min(20, pg_pool_sizes()[0]) | |
| await asyncio.gather(*[_pg_ping() for _ in range(_prewarm)]) | |
| from ..memory.orchestrator import MemoryOrchestrator | |
| orchestrator = await MemoryOrchestrator.create(db_manager) | |
| app.state.orchestrator = orchestrator | |
| # Consolidation has no other automatic entry point: start_background_worker had no | |
| # caller anywhere in the repo, so the sweep only ever ran when someone POSTed to the | |
| # admin route. Off by default — see FeatureFlags.consolidation_scheduler_enabled, | |
| # which also documents the single-worker assumption. | |
| if get_settings().features.consolidation_scheduler_enabled: | |
| await orchestrator.consolidation.start_background_worker() | |
| yield | |
| await orchestrator.consolidation.stop_background_worker() | |
| await db_manager.close() | |
| def create_app() -> FastAPI: | |
| """Create FastAPI application.""" | |
| app = FastAPI( | |
| title="Cognitive Memory Layer", | |
| description="Neuro-inspired memory system for LLMs", | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| async def memory_not_found_handler(_request: Request, exc: MemoryNotFoundError) -> JSONResponse: | |
| return JSONResponse(status_code=404, content={"detail": str(exc)}) | |
| async def memory_access_denied_handler( | |
| _request: Request, exc: MemoryAccessDenied | |
| ) -> JSONResponse: | |
| return JSONResponse(status_code=403, content={"detail": str(exc)}) | |
| async def validation_error_handler(_request: Request, exc: CMLValidationError) -> JSONResponse: | |
| return JSONResponse(status_code=422, content={"detail": str(exc)}) | |
| async def cml_error_handler(_request: Request, exc: CognitiveMemoryError) -> JSONResponse: | |
| return JSONResponse(status_code=400, content={"detail": str(exc)}) | |
| settings = get_settings() | |
| if settings.cors_origins is not None: | |
| origins = settings.cors_origins | |
| elif settings.debug: | |
| origins = ["*"] | |
| else: | |
| origins = ["http://localhost:3000", "http://localhost:8080"] | |
| # CORS spec: credentials are incompatible with wildcard origins | |
| allow_credentials = "*" not in origins | |
| class CSRFCheckMiddleware(BaseHTTPMiddleware): | |
| """Require X-Requested-With for dashboard state-changing requests.""" | |
| async def dispatch(self, request: Request, call_next): | |
| if ( | |
| request.url.path.startswith("/api/v1/dashboard") | |
| and request.method in ("POST", "PUT", "DELETE", "PATCH") | |
| and request.headers.get("X-Requested-With") != "XMLHttpRequest" | |
| ): | |
| return JSONResponse( | |
| status_code=403, | |
| content={"detail": "Missing CSRF header"}, | |
| ) | |
| return await call_next(request) | |
| app.add_middleware(CSRFCheckMiddleware) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=allow_credentials, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| rpm = getattr(settings.auth, "rate_limit_requests_per_minute", 0) | |
| if rpm > 0: | |
| app.add_middleware(RateLimitMiddleware, requests_per_minute=rpm) | |
| app.add_middleware(RequestLoggingMiddleware) | |
| app.include_router(router, prefix="/api/v1") | |
| app.include_router(admin_router, prefix="/api/v1") | |
| app.include_router(dashboard_router, prefix="/api/v1") | |
| # Prometheus metrics endpoint | |
| from prometheus_client import make_asgi_app | |
| metrics_app = make_asgi_app() | |
| app.mount("/metrics", metrics_app) | |
| # Dashboard SPA: serve static files and catch-all for client-side routing | |
| if _DASHBOARD_DIR.is_dir(): | |
| app.mount( | |
| "/dashboard/static", StaticFiles(directory=str(_DASHBOARD_DIR)), name="dashboard-static" | |
| ) | |
| async def dashboard_favicon(): | |
| icon = _DASHBOARD_DIR / "favicon.ico" | |
| if icon.is_file(): | |
| return FileResponse(str(icon)) | |
| return Response(status_code=204) | |
| async def dashboard_spa(rest_of_path: str = ""): | |
| """Serves the dashboard SPA (index.html) for client-side routing. All dashboard routes (e.g. /dashboard/#graph) are handled by this catch-all.""" | |
| index = _DASHBOARD_DIR / "index.html" | |
| if index.is_file(): | |
| return FileResponse(str(index)) | |
| return {"error": "Dashboard not found"} | |
| return app | |
| app = create_app() | |
Xet Storage Details
- Size:
- 6.6 kB
- Xet hash:
- 8346accd2484604bfb0cd1beabf6db2ca72d5c1aacdd835a1860bbde0199313a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.