#session-store #tower-sessions #postgresql #tokio #tokio-postgres #backed

tower-sessions-postgres-store

tower-sessions store backed by tokio-postgres

1 unstable release

0.1.1 Oct 1, 2024

#1122 in Database interfaces

Download history 81/week @ 2024-09-25 69/week @ 2024-10-02 7/week @ 2024-10-09

157 downloads per month

MIT/Apache

17KB
232 lines

Overview

This is a SessionStore for the tower-sessions middleware which uses tokio-postgres for handling Postgres databases. It is directly based on the sqlx-store and inherited the test suite.

Usage

See the counter example for a complete example.

pub fn create_pool(database_url: &str) -> Pool {
    let config = database_url.parse().unwrap();
    let manager = deadpool_postgres::Manager::new(config, tokio_postgres::NoTls);
    deadpool_postgres::Pool::builder(manager).build().unwrap()
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // create the session store and run it's migration
    let database_url = std::option_env!("DATABASE_URL").expect("Missing DATABASE_URL.");
    let pool = create_pool(database_url);
    let session_store = PostgresStore::new(pool);
    session_store.migrate().await?;

    // create the session layer
    let session_layer = SessionManagerLayer::new(session_store)
        .with_secure(false)
        .with_expiry(Expiry::OnInactivity(Duration::seconds(10)));

    // wire it up with axum...
    let app = Router::new().route("/", get(handler)).layer(session_layer);
    // ...
}

and use it as such:

const COUNTER_KEY: &str = "counter";

#[derive(Serialize, Deserialize, Default)]
struct Counter(usize);

async fn handler(session: Session) -> impl IntoResponse {
    let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
    session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
    format!("Current count: {}", counter.0)
}

Dependencies

~9–19MB
~269K SLoC