gsm_session/
lib.rs

1use anyhow::Result;
2pub use greentic_session::{SessionData, SessionKey};
3use greentic_session::{SessionStore, inmemory::InMemorySessionStore};
4use greentic_types::{TenantCtx, UserId};
5use std::sync::Arc;
6
7/// Shared session store handle that wraps the greentic-session backends.
8#[derive(Clone)]
9pub struct SharedSessionStore {
10    inner: Arc<InMemorySessionStore>,
11}
12
13/// Builds a session store from environment configuration.
14pub async fn store_from_env() -> Result<SharedSessionStore> {
15    Ok(shared_memory_store())
16}
17
18/// Returns an in-memory session store.
19pub fn shared_memory_store() -> SharedSessionStore {
20    SharedSessionStore {
21        inner: Arc::new(InMemorySessionStore::new()),
22    }
23}
24
25impl SharedSessionStore {
26    /// Looks up the active session bound to the provided tenant + user combination.
27    pub async fn find_by_user(
28        &self,
29        ctx: &TenantCtx,
30        user: &UserId,
31    ) -> Result<Option<(SessionKey, SessionData)>> {
32        self.inner.find_by_user(ctx, user).map_err(Into::into)
33    }
34
35    /// Creates a new session and returns its key.
36    pub async fn create_session(&self, ctx: &TenantCtx, data: SessionData) -> Result<SessionKey> {
37        self.inner.create_session(ctx, data).map_err(Into::into)
38    }
39
40    /// Updates an existing session with the supplied snapshot.
41    pub async fn update_session(&self, key: &SessionKey, data: SessionData) -> Result<()> {
42        self.inner.update_session(key, data).map_err(Into::into)
43    }
44}