"""Shared FastAPI dependencies. The :class:`ServiceState` and :class:`SyncConfig` are created once in ``service.run`` and injected into routes via ``Depends``, avoiding global singletons. Routes get a ready-to-use :class:`HealthChecker` rather than the raw state, so each endpoint declares only what it needs. """ from __future__ import annotations from fastapi import Depends from ..config import SyncConfig from ..health import HealthChecker, ServiceState # Bound at app creation time (create_app). Module-level holders are acceptable # here because there is exactly one app per process and they are write-once. _state: ServiceState | None = None _cfg: SyncConfig | None = None def bind(state: ServiceState, cfg: SyncConfig) -> None: """Stash the singletons for the lifetime of this app (called once).""" global _state, _cfg _state = state _cfg = cfg def get_state() -> ServiceState: assert _state is not None, "ServiceState not bound -- call bind() first" return _state def get_config() -> SyncConfig: assert _cfg is not None, "SyncConfig not bound -- call bind() first" return _cfg def get_checker( state: ServiceState = Depends(get_state), cfg: SyncConfig = Depends(get_config), ) -> HealthChecker: return HealthChecker(state, cfg)