Expand description

Axum_database_Sessions

Library to Provide a Sqlx Database Session management layer. You must also include Tower_cookies in order to use these Library.

You must choose only one of [‘postgres’, ‘mysql’, ‘sqlite’] features to use this library.

https://crates.io/crates/axum_database_sessions Docs

Install

Axum Database Sessions uses tokio runtime along with [‘sqlx’]; it supports native-tls and rustls TLS backends. When adding the dependency, you must chose a database feature that is DatabaseType and a tls backend. You can only choose one database type and one TLS Backend.

[dependencies]
axum_database_sessions = { version = "0.2", features = [ "postgres", "rustls"] }
Cargo Feature Flags

sqlite: Sqlx support for the self-contained SQLite database engine. postgres: Sqlx support for the Postgres database server. mysql: Sqlx support for the MySQL/MariaDB database server. native-tls: Use the tokio runtime and native-tls TLS backend. rustls: Use the tokio runtime and rustls TLS backend.

Example

use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_database_sessions::{AxumSession, AxumSessionConfig, AxumSessionStore, axum_session_runner};
use axum::{
    Router,
    routing::get,
};

#[tokio::main]
async fn main() {
    let poll = connect_to_database().await.unwrap();

    let session_config = AxumSessionConfig::default()
        .with_database("test")
        .with_table_name("test_table");

    let session_store = AxumSessionStore::new(Some(poll.clone().into()), session_config);

    // build our application with some routes
    let app = Router::new()
        .route("/greet/:name", get(greet))
        .layer(tower_cookies::CookieManagerLayer::new())
        .layer(axum_session_runner!(session_store));

    // run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn greet(session: AxumSession) -> String {
    let mut count: usize = session.get("count").await.unwrap_or(0);
    count += 1;
    session.set("count", count).await;

    count.to_string()
}

async fn connect_to_database() -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
    // ...
}

To use Axum_database_session in non_persistant mode Set the client to None.

Example

use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_database_sessions::{AxumSession, AxumSessionConfig, AxumSessionStore, axum_session_runner};
use axum::{
    Router,
    routing::get,
};

#[tokio::main]
async fn main() {
    let session_config = AxumSessionConfig::default()
        .with_database("test")
        .with_table_name("test_table");

    let session_store = AxumSessionStore::new(None, session_config);

    // build our application with some routes
    let app = Router::new()
        .route("/greet/:name", get(greet))
        .layer(tower_cookies::CookieManagerLayer::new())
        .layer(axum_session_runner!(session_store));

    // run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

Re-exports

pub extern crate axum_extra;

Macros

Used to Setup the Layer for Axum_Sessions.

Structs

Mysql’s Pool type for AxumDatabasePool

This is the Session that is generated when a user is routed to a page that Needs one It is used to Save and load session data similar to how it is done on python.

This is the Sessions Config it is used to Setup the SQL database and sets the hashmap saved Memory and Session life spans.

This Contains all of out Sessions Data including their Hashed Data they access.

This Contains the ID of the Session which is stored in a Cookie and in the Main SessionStore Hash to find their SessionData

This stores the Postgresql Pool and the Main timers and a hash table that stores the SessionData. It is also used to Initiate a Database Migrate, Cleanup, etc when used directly.

Internal Timers to keep track of when last ran an expiry or database sweep.

Enums

The SameSite cookie attribute.

Functions

axum_session_manager_run Creates, Manages and Sets an AxumSession into the Request extensions. This will unload and load other Session data based on Access and a timer check. returns an Response when all the Futures After run.

Create a middleware from an async function.