Crates.io | tower-sessions-postgres-store |
lib.rs | tower-sessions-postgres-store |
version | 0.1.1 |
source | src |
created_at | 2024-10-01 16:09:32.509988 |
updated_at | 2024-10-01 16:09:32.509988 |
description | tower-sessions store backed by tokio-postgres |
homepage | |
repository | |
max_upload_size | |
id | 1393091 |
size | 64,985 |
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.
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)
}