Use axum_session_surreal for SurrealDB persistence
mainaxum_session_surreal provides a persistent storage backend for the axum_session framework using SurrealDB. This allows session data to be stored in a Surreal database rather than in-memory.repository·main·Indexed 41 days ago
https://github.com/ascendingcreations/axumsessionA high-performance session management middleware for the Axum web framework (v0.21.0). It supports HTTP and REST modes, providing flexible storage options including in-memory, Redis, MongoDB, SurrealDB, and SQLx. Key features include signed cookies, data encryption, SessionID renewal, and performance optimization via Fastbloom key storage.
axum_session_surreal provides a persistent storage backend for the axum_session framework using SurrealDB. This allows session data to be stored in a Surreal database rather than in-memory.axum_session_mongo provides a NoSQL persistent storage implementation for the axum_session framework, allowing you to store Axum sessions in a MongoDB database.To reduce database lookups and improve bandwidth/latency, enable the key-store feature. This uses Fastbloom for in-process key storage.
Configuration:
use_bloom_filters option is enabled by default.with_filter_expected_elements and with_filter_false_positive_probability in your SessionConfig.Axum Session is a middleware for Axum that manages session data.
Core Mechanics:
MemoryStore (via dashmap for high throughput) and can optionally sync to a persistent database for long-term storage.Serde for serialization, allowing you to store any Serde-supported types in the session.DatabasePool trait, allowing you to implement your own sub-storage layer.To use axum_session with a persistent SQLx database, follow this pattern:
SessionConfig (e.g., setting a table name).SessionStore with the database pool and config.SessionLayer to your Axum Router.Session<T> extractor.use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{Session, SessionPgPool, SessionConfig, SessionStore, SessionLayer};
use axum::{
Router,
routing::get,
};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let pool = connect_to_database().await.unwrap();
// This Defaults as normal Cookies.
let session_config = SessionConfig::default()
.with_table_name("sessions_table");
// create SessionStore and initiate the database tables
let session_store = SessionStore::<SessionPgPool>::new(Some(pool.clone().into()), session_config).await.unwrap();
// build our application with some routes
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
// run it
let addr = SocketAddr::from(([0, 0, 0, 0], 8000));
println!("listening on {}", addr);
let listener = TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn greet(session: Session<SessionPgPool>) -> String {
let mut count: usize = session.get("count").unwrap_or(0);
count += 1;
session.set("count", count);
count.to_string()
}
async fn connect_to_database() -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
// ...
unimplemented!()
}To use the Redis BB8 pool store with AxumSession, add both axum_session and axum_session_redis_bb8_pool to your Cargo.toml dependencies. Ensure you use compatible versions as specified in the documentation.
# Cargo.toml
[dependencies]
axum_session = { version = "0.21.0" }
axum_session_redis_bb8_pool = { version = "0.9.0" }Add axum_session to your Cargo.toml. Note that axum_session requires tokio to be included in your project dependencies.
# Cargo.toml
[dependencies]
axum_session = { version = "0.19.0" }To use MongoDB as a persistent store for AxumSession, add both axum_session and axum_session_mongo to your Cargo.toml dependencies. Ensure you use compatible versions as specified in the documentation.
# Cargo.toml
[dependencies]
axum_session = { version = "0.21.0" }
axum_session_mongo = { version = "0.9.0" }To use SurrealDB as a persistent store for AxumSession, add both axum_session and axum_session_surreal to your Cargo.toml dependencies.
[dependencies]
axum_session = { version = "0.21.0" }
axum_session_surreal = { version = "0.9.0" }To use axum_session_redispool as a Redis pool store for axum_session, add the following dependencies to your Cargo.toml file:
[dependencies]
axum_session = { version = "0.21.0" }
axum_session_redispool = { version = "0.12.0" }The axum_session_redispool crate provides a session storage backend for axum-session using a Redis connection pool. It allows you to manage user sessions in Redis, which is suitable for distributed applications requiring shared session state.
By default, it exports the necessary types and functions from redis_pool. If the redis-clusterdb feature is enabled, it also provides support for Redis Cluster environments via redis_cluster_pool.
// Note: This is a library entrypoint.
// Usage typically involves importing the session store from this crate
// and passing it to the axum-session middleware configuration.By default, axum_session_redispool is configured for standard Redis instances. To enable support for Redis Clusters, enable the redis-clusterdb feature in your Cargo.toml.
axum_session_redispool = { version = "0.12.0", features = ["redis-clusterdb"] }