Skip to main content

crabllm_proxy/
state.rs

1use crabllm_core::{Extension, GatewayConfig, Provider, Storage};
2use crabllm_provider::ProviderRegistry;
3use std::{
4    collections::HashMap,
5    sync::{Arc, RwLock},
6};
7
8/// Shared application state passed to all handlers.
9///
10/// Generic over the storage backend `S` and the provider type `P`. The
11/// binary picks `P` by defining a workspace-level union enum that wraps
12/// every provider source it links — that enum implements `Provider` via
13/// match-and-delegate, so dispatch through `P` is fully monomorphized.
14pub struct AppState<S: Storage, P: Provider> {
15    pub registry: ProviderRegistry<P>,
16    pub config: GatewayConfig,
17    pub extensions: Arc<Vec<Box<dyn Extension>>>,
18    pub storage: Arc<S>,
19    /// Precomputed token → key name lookup for O(1) auth.
20    /// Wrapped in RwLock to support runtime key management.
21    pub key_map: Arc<RwLock<HashMap<String, String>>>,
22}
23
24impl<S: Storage, P: Provider> Clone for AppState<S, P> {
25    fn clone(&self) -> Self {
26        Self {
27            registry: self.registry.clone(),
28            config: self.config.clone(),
29            extensions: self.extensions.clone(),
30            storage: self.storage.clone(),
31            key_map: self.key_map.clone(),
32        }
33    }
34}