tower-sessions-deadpool-sqlite-store

Crates.iotower-sessions-deadpool-sqlite-store
lib.rstower-sessions-deadpool-sqlite-store
version0.1.1
sourcesrc
created_at2024-05-23 13:27:45.669545
updated_at2024-05-23 13:30:24.494232
descriptiondeadpool-sqlite session store for `tower-sessions`
homepagehttps://github.com/cs2dsb/tower-sessions-deadpool-sqlite-store.rs
repository
max_upload_size
id1249489
size15,209
(cs2dsb)

documentation

https://docs.rs/tower-sessions-deadpool-sqlite-store

README

deadpool-sqlite session store for tower-sessions

Crate Documentation

An implementation of SessionStore from tower-sessions that uses deadpool-sqlite as the backing store.

It currently uses serde_json for serializing the session because I wanted them to be human readable for debugging purposes but it could be adapted to use something more compact if performance is a concern.

Usage

// Create the deadpool-sqlite database pool 
let pool = Config::new(args.sqlite_connection_string)
    .builder(Runtime::Tokio1)?
    // This is not necessary for the session store but I've left it in because it was hard to find
    // an example of using post_create
    .post_create(Hook::async_fn(|object, _| {
        Box::pin(async move {
            object
                .interact(|conn| db::configure_new_connection(conn))
                .await
                .map_err(AppError::from)?
                .map_err(AppError::from)?;
            Ok(())
        })
    }))
    .build()?;

// Create the session store
let session_store = DeadpoolSqliteStore::new(pool.clone());
// Call migrate to create the session table if it doesn't exist
session_store.migrate().await?;


axum::serve(
    ...
    ...
    .layer(
        // Pass the session_store to the session manager layer
        SessionManagerLayer::new(session_store)
            .with_secure(args.secure_sessions)
            .with_expiry(Expiry::OnInactivity(Duration::days(
                args.session_expiry_days,
            ))),
    ),
    ...
    ...
)
.await?;

Disclaimer

This was created for my own usage in a hobby project so support may be spotty. Feel free to raise issues but don't depend on it for any mission critical applications.

Commit count: 0

cargo fmt