Crates.io | tower-sessions-deadpool-sqlite-store |
lib.rs | tower-sessions-deadpool-sqlite-store |
version | 0.1.1 |
source | src |
created_at | 2024-05-23 13:27:45.669545 |
updated_at | 2024-05-23 13:30:24.494232 |
description | deadpool-sqlite session store for `tower-sessions` |
homepage | https://github.com/cs2dsb/tower-sessions-deadpool-sqlite-store.rs |
repository | |
max_upload_size | |
id | 1249489 |
size | 15,209 |
tower-sessions
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.
// 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?;
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.