1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

update example redis-session (#545)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Koki Nishihara 2022-07-21 11:21:07 +09:00 committed by GitHub
parent b6255d1637
commit 5c9f69bb07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 226 additions and 147 deletions

349
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,11 @@
[package] [package]
name = "redis_session" name = "redis-session-example"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-session = "0.5" actix-session = { version = "0.7", features = ["redis-actor-session"] }
actix-redis = "0.10"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"

View File

@ -4,8 +4,7 @@
//! At login, the session key changes and session state in cache re-assigns. //! At login, the session key changes and session state in cache re-assigns.
//! At logout, session state in cache is removed and cookie is invalidated. //! At logout, session state in cache is removed and cookie is invalidated.
//! //!
use actix_redis::RedisSession; use actix_session::{storage::RedisActorSessionStore, Session, SessionMiddleware};
use actix_session::Session;
use actix_web::{ use actix_web::{
middleware, web, middleware, web,
web::{get, post, resource}, web::{get, post, resource},
@ -85,7 +84,13 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
// redis session middleware // redis session middleware
.wrap(RedisSession::new("127.0.0.1:6379", private_key.master())) .wrap(
SessionMiddleware::builder(
RedisActorSessionStore::new("127.0.0.1:6379"),
private_key.clone(),
)
.build(),
)
// enable logger - always register Actix Web Logger middleware last // enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index))) .service(resource("/").route(get().to(index)))
@ -115,8 +120,12 @@ mod test {
let srv = actix_test::start(move || { let srv = actix_test::start(move || {
App::new() App::new()
.wrap( .wrap(
RedisSession::new("127.0.0.1:6379", private_key.master()) SessionMiddleware::builder(
.cookie_name("test-session"), RedisActorSessionStore::new("127.0.0.1:6379"),
private_key.clone(),
)
.cookie_name("test-session".to_string())
.build(),
) )
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index))) .service(resource("/").route(get().to(index)))