20 releases

new 0.5.0-beta.3 Oct 22, 2021
0.5.0-beta.2 Jun 27, 2021
0.5.0-beta.1 Apr 2, 2021
0.4.1 Mar 21, 2021
0.1.0-alpha.2 Mar 30, 2019

#3 in #session

Download history 1448/week @ 2021-07-02 2099/week @ 2021-07-09 2103/week @ 2021-07-16 1995/week @ 2021-07-23 2094/week @ 2021-07-30 2250/week @ 2021-08-06 1578/week @ 2021-08-13 1882/week @ 2021-08-20 1560/week @ 2021-08-27 1448/week @ 2021-09-03 2306/week @ 2021-09-10 2616/week @ 2021-09-17 2195/week @ 2021-09-24 2306/week @ 2021-10-01 2860/week @ 2021-10-08 3260/week @ 2021-10-15

9,400 downloads per month
Used in 23 crates (17 directly)

MIT/Apache

36KB
682 lines

actix-session

Sessions for Actix Web.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Documentation & Resources


lib.rs:

Sessions for Actix Web.

Provides a general solution for session management. Session middleware could provide different implementations which could be accessed via general session API.

This crate provides a general solution for session management and includes a cookie backend. Other backend implementations can be built to use persistent or key-value stores, for example.

In general, some session middleware, such as a [CookieSession] is initialized and applied. To access session data, the [Session] extractor must be used. This extractor allows reading modifying session data.

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_session::{Session, CookieSession};

fn index(session: Session) -> Result<&'static str, Error> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        println!("SESSION value: {}", count);
        session.insert("counter", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }

    Ok("Welcome!")
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(
        || App::new()
            // create cookie based session middleware
            .wrap(CookieSession::signed(&[0; 32]).secure(false))
            .default_service(web::to(|| HttpResponse::Ok())))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

Dependencies

~22MB
~514K SLoC