Skip to main content

lago_api/
state.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3use std::sync::Arc;
4use std::time::Instant;
5
6use lago_auth::AuthLayer;
7use lago_core::{Journal, ManifestEntry};
8use lago_policy::{HookRunner, PolicyEngine, RbacManager};
9use lago_store::BlobStore;
10use metrics_exporter_prometheus::PrometheusHandle;
11use tokio::sync::RwLock;
12
13/// Cached manifest entry with expiration timestamp.
14pub struct CachedManifest {
15    pub entries: Vec<ManifestEntry>,
16    pub cached_at: Instant,
17}
18
19/// Default manifest cache TTL: 60 seconds.
20pub const MANIFEST_CACHE_TTL_SECS: u64 = 60;
21
22/// Shared application state threaded through all axum handlers.
23///
24/// Wrapped in `Arc` so it can be cheaply cloned into every request.
25pub struct AppState {
26    /// Event journal (session + event persistence).
27    pub journal: Arc<dyn Journal>,
28    /// Content-addressed blob store.
29    pub blob_store: Arc<BlobStore>,
30    /// Root path for filesystem data.
31    pub data_dir: PathBuf,
32    /// Daemon startup time for uptime reporting.
33    pub started_at: Instant,
34    /// Auth layer for JWT-protected routes (None = auth disabled).
35    pub auth: Option<Arc<AuthLayer>>,
36    /// Rule-based policy engine for tool governance (None = no enforcement).
37    pub policy_engine: Option<Arc<PolicyEngine>>,
38    /// RBAC manager for session-to-role assignments (None = no RBAC).
39    /// Wrapped in RwLock to support runtime role assignments.
40    pub rbac_manager: Option<Arc<RwLock<RbacManager>>>,
41    /// Hook runner for pre/post operation hooks (None = no hooks).
42    pub hook_runner: Option<Arc<HookRunner>>,
43    /// Rate limiter for public endpoints (None = no rate limiting).
44    pub rate_limiter: Option<Arc<crate::rate_limit::RateLimiter>>,
45    /// Prometheus metrics handle for rendering the `/metrics` endpoint.
46    pub prometheus_handle: PrometheusHandle,
47    /// In-memory manifest cache keyed by (session_id, branch_id).
48    /// Avoids replaying all journal events on every file/manifest request.
49    pub manifest_cache: RwLock<HashMap<(String, String), CachedManifest>>,
50}