1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +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]
name = "redis_session"
name = "redis-session-example"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4"
actix-session = "0.5"
actix-redis = "0.10"
actix-session = { version = "0.7", features = ["redis-actor-session"] }
env_logger = "0.9"
log = "0.4"

View File

@ -4,8 +4,7 @@
//! 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.
//!
use actix_redis::RedisSession;
use actix_session::Session;
use actix_session::{storage::RedisActorSessionStore, Session, SessionMiddleware};
use actix_web::{
middleware, web,
web::{get, post, resource},
@ -85,7 +84,13 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
// 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
.wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index)))
@ -115,8 +120,12 @@ mod test {
let srv = actix_test::start(move || {
App::new()
.wrap(
RedisSession::new("127.0.0.1:6379", private_key.master())
.cookie_name("test-session"),
SessionMiddleware::builder(
RedisActorSessionStore::new("127.0.0.1:6379"),
private_key.clone(),
)
.cookie_name("test-session".to_string())
.build(),
)
.wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index)))