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

tower-sessions-deadpool-sqlite-store

tower-sessions提供的deadpool-sqlite会话存储

2个版本

0.1.1 2024年5月23日
0.1.0 2024年5月23日

#1886异步

MIT许可证

13KB
201

tower-sessions提供的deadpool-sqlite会话存储

Crate Documentation

使用SessionStore实现,来自tower-sessions,并使用deadpool-sqlite作为后端存储。

它目前使用serde_json序列化会话,因为我希望它们在调试时易于阅读,但可以根据性能需求调整以使用更紧凑的格式。

用法

// 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?;

免责声明

此库是为我在个人爱好项目中使用而创建的,因此可能支持有限。请随时提出问题,但不要将其用于任何关键任务应用。

依赖

~36MB
~575K SLoC