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#[derive(Clone)]
9pub struct SharedSessionStore {
10 inner: Arc<InMemorySessionStore>,
11}
12
13pub async fn store_from_env() -> Result<SharedSessionStore> {
15 Ok(shared_memory_store())
16}
17
18pub fn shared_memory_store() -> SharedSessionStore {
20 SharedSessionStore {
21 inner: Arc::new(InMemorySessionStore::new()),
22 }
23}
24
25impl SharedSessionStore {
26 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 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 pub async fn update_session(&self, key: &SessionKey, data: SessionData) -> Result<()> {
42 self.inner.update_session(key, data).map_err(Into::into)
43 }
44}