5 个版本

0.2.0 2023 年 11 月 19 日
0.1.3 2023 年 11 月 18 日
0.1.2 2023 年 11 月 18 日
0.1.1 2023 年 11 月 18 日
0.1.0 2023 年 11 月 18 日

#811HTTP 服务器

29 每月下载次数

MIT 许可证

85KB
1.5K SLoC

actix-extended-session

为 Actix Web 提供扩展会话管理。

actix-session 的修改版。

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

文档与资源

  • API 文档
  • 最低支持的 Rust 版本 (MSRV):1.57

lib.rs:

为 Actix Web 提供扩展会话管理。

actix-extended-session 提供了一个易于使用的框架,用于管理在 Actix Web 上构建的应用程序中的会话。 SessionMiddlewareactix-extended-session 提供的功能的基础中间件;它负责所有会话cookie的处理,并根据对活动 Session 执行的操作,指导 存储后端 创建/删除/更新会话状态。

有关会话的进一步阅读

入门指南

要在您的 Actix Web 应用程序中使用会话,您必须将 SessionMiddleware 注册为 App 上的中间件。

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_extended_session::{Session, SessionMiddleware, storage::CookieSessionStore};
use actix_web::cookie::Key;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // The secret key would usually be read from a configuration file/environment variables.
    let secret_key = Key::generate();
    HttpServer::new(move ||
            App::new()
            // Add session management to your application using Redis for session state storage
            .wrap(
                SessionMiddleware::new(
                    CookieSessionStore::default(),
                    secret_key.clone()
                )
            )
            .default_service(web::to(|| HttpResponse::Ok())))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

您的请求处理程序可以使用 Session 提取器访问和修改会话状态。请注意,这在流式响应的流中不起作用。

use actix_web::Error;
use actix_extended_session::Session;
use serde_json::Value;

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

    Ok("Welcome!")
}

依赖关系

~15–28MB
~506K SLoC