#tower-sessions #tower #deadpool #session #axum #rusqlite

tower-sessions-deadpool-sqlite-store

deadpool-sqlite session store for tower-sessions

2 releases

0.1.1 May 23, 2024
0.1.0 May 23, 2024

#2078 in Asynchronous

MIT license

13KB
201 lines

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.

Dependencies

~36MB
~577K SLoC