Skip to main content

greentic_session/
lib.rs

1#![forbid(unsafe_code)]
2
3mod backends;
4
5pub mod error;
6pub mod inmemory;
7pub mod mapping;
8pub mod store;
9
10pub use error::{ErrorCode, GreenticError, SessionResult};
11pub use greentic_types::{ReplyScope, SessionData, SessionKey};
12pub use store::SessionStore;
13
14/// Configuration for selecting a session backend.
15#[derive(Debug, Clone)]
16pub enum SessionBackendConfig {
17    /// In-memory store for tests or single-node development.
18    InMemory,
19    /// Redis-backed store using the default namespace.
20    #[cfg(feature = "redis")]
21    RedisUrl(String),
22    /// Redis-backed store with a custom namespace prefix.
23    #[cfg(feature = "redis")]
24    RedisUrlWithNamespace { url: String, namespace: String },
25}
26
27/// Creates a boxed session store using the provided backend configuration.
28pub fn create_session_store(config: SessionBackendConfig) -> SessionResult<Box<dyn SessionStore>> {
29    match config {
30        SessionBackendConfig::InMemory => Ok(Box::new(inmemory::InMemorySessionStore::new())),
31        #[cfg(feature = "redis")]
32        SessionBackendConfig::RedisUrl(url) => {
33            let store = backends::redis::RedisSessionStore::from_url(&url)?;
34            Ok(Box::new(store))
35        }
36        #[cfg(feature = "redis")]
37        SessionBackendConfig::RedisUrlWithNamespace { url, namespace } => {
38            let store =
39                backends::redis::RedisSessionStore::from_url_with_namespace(&url, namespace)?;
40            Ok(Box::new(store))
41        }
42    }
43}