1 unstable release

0.1.0 Mar 5, 2022

#137 in #web-framework

MIT license

30KB
537 lines

Session for actix web framework Build Status codecov crates.io Join the chat at https://gitter.im/actix/actix

This crate moved to https://github.com/actix/actix-extras.

Documentation & community resources


lib.rs:

User sessions.

Actix provides a general solution for session management. Session middlewares could provide different implementations which could be accessed via general session api.

By default, only cookie session backend is implemented. Other backend implementations can be added.

In general, you insert a session middleware and initialize it , such as a CookieSessionBackend. To access session data, Session extractor must be used. Session extractor allows us to get or set session data.

use ntex::web::{self, App, HttpResponse, Error};
use ntex_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.set("counter", count+1)?;
    } else {
        session.set("counter", 1)?;
    }

    Ok("Welcome!")
}

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

Dependencies

~10–16MB
~386K SLoC