"""FastAPI entry point — wires lifespan, routers, error envelope handlers.""" from __future__ import annotations import logging from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app import __version__ from app.api import ( routes_cameras, routes_hls, routes_metrics_ws, routes_scan, ) from app.config import settings from app.core.errors import install as install_errors from app.core.logging import setup_logging from app.core.openapi import custom_openapi from app.services.registry import registry setup_logging(settings.log_level) log = logging.getLogger("app") @asynccontextmanager async def lifespan(app: FastAPI): log.info("starting Cakra CCTV-YOLO Analytics v%s", __version__) # Surface the active config profile + the resource-sensitive knobs at # boot so HF Spaces / local-profile-test runs are easy to verify. import os as _os _profile = _os.environ.get("CAKRA_PROFILE") or ("hfspace-auto" if _os.environ.get("SPACE_ID") else "local") log.info( "config profile=%s | inference=%dpx fps_target=%.1f frame_skip=%d output=%dx%d max_streams=%d", _profile, settings.inference_size, settings.target_infer_fps, settings.frame_skip, settings.output_width, settings.output_height, settings.max_streams, ) log.info( "scan mode | max_scan_streams=%d target_fps=%.1f ring=%.0fs " "scan_output=%dx%d alerts_dir=%s", settings.max_scan_streams, settings.scan_target_fps, settings.scan_ring_buffer_seconds, settings.scan_output_width, settings.scan_output_height, settings.alerts_dir, ) app.state.registry = registry app.state.api_prefix = settings.api_prefix registry.startup() try: yield finally: registry.shutdown() app = FastAPI( title="Cakra Intelligence — CCTV YOLO Analytics", summary="Realtime multi-camera CCTV analytics: embeddable MJPEG, " "REST metrics, WebSocket pushes, and an events log.", description=( "Each camera exposes an embeddable MJPEG stream with YOLO + " "ByteTrack overlays, REST endpoints for current/rollup metrics, " "a WebSocket channel for live metric pushes, and an incidents " "log backed by TimescaleDB." ), version=__version__, contact={"name": "Cakra Intelligence", "email": "openscopelabs@gmail.com"}, license_info={"name": "Proprietary", "identifier": "LicenseRef-Proprietary"}, openapi_url="/openapi.json", docs_url="/docs", redoc_url="/redoc", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["*"], ) install_errors(app) app.openapi = custom_openapi(app) @app.get("/", tags=["Meta"], summary="Service metadata.") async def root() -> dict: return { "service": "cakra-cctv-yolo-analytics", "version": __version__, "docs": "/docs", "openapi": "/openapi.json", "api_prefix": settings.api_prefix, } @app.get("/health", tags=["Meta"], summary="Liveness probe.") async def health() -> dict: return { "status": "ok", "version": __version__, "cameras": len(registry.workers), } app.include_router(routes_cameras.router, prefix=settings.api_prefix) app.include_router(routes_hls.router, prefix=settings.api_prefix) app.include_router(routes_metrics_ws.router, prefix=settings.api_prefix) app.include_router(routes_scan.scan_router, prefix=settings.api_prefix) app.include_router(routes_scan.alerts_router, prefix=settings.api_prefix)