Crate axum_sqlx_sessions[][src]

Expand description

Axum_Sqlx_Sessions

Library to Provide a Postgresql Session management layer. You must also include Tower_cookies in order to use this Library.

https://crates.io/crates/axum_sqlx_sessions Docs

Example

pub fn init_pool(config: &ServerConfig) -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
    let mut connect_opts = PgConnectOptions::new();
    connect_opts.log_statements(LevelFilter::Debug);
    connect_opts = connect_opts.database(&config.pgsql_database[..]);
    connect_opts = connect_opts.username(&config.pgsql_user[..]);
    connect_opts = connect_opts.password(&config.pgsql_password[..]);
    connect_opts = connect_opts.host(&config.pgsql_host[..]);
    connect_opts = connect_opts.port(config.pgsql_port);

    let pool = block_on(
        PgPoolOptions::new()
            .max_connections(5)
            .connect_with(connect_opts),
    )?;

    Ok(pool)
}

#[tokio::main]
async fn main() {
    // Set the RUST_LOG, if it hasn't been explicitly defined
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "example_templates=debug,tower_http=debug")
    }
    tracing_subscriber::fmt::init();

    let config = //load your config here.
    let poll = init_pool(&config).unwrap();

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

    // build our application with some routes
    let app = Router::new()
        .route("/greet/:name", get(greet))
        .layer(tower_cookies::CookieManagerLayer::new())
        .layer(SqlxSessionLayer::new(session_config, poll.clone()))

    // 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: SQLxSession) -> &'static str {
    let mut count: usize = session.get("count").unwrap_or(0);
    count += 1;
    session.set("count", count);

    count.to_string()[..]
}

Structs

Response future for [SessionManager].

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 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 manages the other services that can be seen in inner and gives access to the store. the store is cloneable hence per each SQLxSession we clone it as we use thread Read write locks to control any data that needs to be accessed across threads that cant be cloned.

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.

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

Session layer struct used for starting the Manager when a user comes on board.