Skip to main content

notedthat_api_http/
state.rs

1//! Shared application state for the axum router.
2
3use notedthat_core::{AccessPolicy, KbSlug, Storage};
4use notedthat_indexer::Searcher;
5use std::collections::BTreeMap;
6use std::sync::Arc;
7
8/// Application state shared across all axum handlers.
9///
10/// This is cloned cheaply for each request (all fields are behind [`Arc`]).
11#[derive(Clone)]
12pub struct AppState {
13    /// The backing storage implementation (injected at startup).
14    pub storage: Arc<dyn Storage>,
15    /// Canonical map of slug string → [`KbSlug`] for declared knowledge bases.
16    pub declared_kbs: Arc<BTreeMap<String, KbSlug>>,
17    /// Startup snapshot of manifest access policies, keyed by KB slug.
18    ///
19    /// One `Arc` per policy so a request-scoped authorization handle can hold
20    /// its knowledge base's policy cheaply instead of borrowing the whole map.
21    pub access_policies: Arc<BTreeMap<String, Arc<AccessPolicy>>>,
22    /// Static Bearer token for authenticating API requests.
23    pub bearer_token: Arc<String>,
24    /// Maximum accepted PUT body size in bytes (16 MiB in M2).
25    pub max_body_size: u64,
26    /// Maximum object size eligible for patch operations, in bytes.
27    pub max_patchable_size: u64,
28    /// Sender half of the async indexing queue.
29    pub indexer_tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>,
30    /// The search implementation (injected at startup).
31    pub searcher: Arc<dyn Searcher>,
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::testing::InMemoryStorage;
38    use std::collections::BTreeMap;
39    use std::sync::Arc;
40
41    fn minimal_state(tx: tokio::sync::mpsc::Sender<notedthat_indexer::IndexEvent>) -> AppState {
42        AppState {
43            storage: Arc::new(InMemoryStorage::default()),
44            declared_kbs: Arc::new(BTreeMap::new()),
45            access_policies: Arc::new(BTreeMap::new()),
46            bearer_token: Arc::new("token".to_string()),
47            max_body_size: 1024,
48            max_patchable_size: 1024,
49            indexer_tx: tx,
50            searcher: Arc::new(crate::testing::NoopSearcher),
51        }
52    }
53
54    #[tokio::test]
55    async fn clone_shares_same_channel() {
56        let (tx, mut rx) = tokio::sync::mpsc::channel(10);
57        let state = minimal_state(tx);
58        let cloned = state.clone();
59
60        let event = notedthat_indexer::IndexEvent::Tombstone {
61            kb: notedthat_core::KbSlug::try_new("test").expect("valid kb slug"),
62            object_key: notedthat_core::ObjectPath::try_from("a.md").expect("valid path"),
63        };
64        cloned
65            .indexer_tx
66            .send(event.clone())
67            .await
68            .expect("send on cloned tx");
69
70        let received = rx.recv().await.expect("receive on original rx");
71        assert_eq!(
72            received, event,
73            "cloned Sender must share the same underlying channel"
74        );
75    }
76}