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

chore: use redis-rs in session features

This commit is contained in:
Rob Ede 2024-05-25 03:58:03 +01:00
parent dd102ae65b
commit f03f126d4f
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 11 additions and 66 deletions

51
Cargo.lock generated
View File

@ -292,27 +292,6 @@ dependencies = [
"prost",
]
[[package]]
name = "actix-redis"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38446dc11c743f4f0023b1067c7cfb8f2548f24418e31a193b324e35fa059279"
dependencies = [
"actix",
"actix-rt",
"actix-service",
"actix-tls",
"actix-web",
"backoff",
"derive_more",
"futures-core",
"log",
"redis-async",
"time",
"tokio 1.37.0",
"tokio-util 0.7.10",
]
[[package]]
name = "actix-router"
version = "0.5.3"
@ -373,15 +352,13 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b671404ec72194d8af58c2bdaf51e3c477a0595056bd5010148405870dda8df2"
dependencies = [
"actix",
"actix-redis",
"actix-service",
"actix-utils",
"actix-web",
"anyhow",
"derive_more",
"futures-core",
"rand 0.8.5",
"redis",
"serde",
"serde_json",
"tracing",
@ -1586,17 +1563,6 @@ dependencies = [
"tokio-util 0.7.10",
]
[[package]]
name = "backoff"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1"
dependencies = [
"getrandom 0.2.12",
"instant",
"rand 0.8.5",
]
[[package]]
name = "backtrace"
version = "0.3.69"
@ -6031,21 +5997,6 @@ dependencies = [
"url",
]
[[package]]
name = "redis-async"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2777130e406c74c28b6cddc0194fcdc2553b5a8795eef9f6384bd3b70a07ba3f"
dependencies = [
"bytes 1.6.0",
"futures-channel",
"futures-sink",
"futures-util",
"log",
"tokio 1.37.0",
"tokio-util 0.7.10",
]
[[package]]
name = "redis-session-example"
version = "1.0.0"

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
actix-web.workspace = true
actix-session = { workspace = true, features = ["redis-actor-session"] }
actix-session = { workspace = true, features = ["redis-rs-session"] }
env_logger.workspace = true
log.workspace = true

View File

@ -3,8 +3,8 @@
//! Every request gets a session, corresponding to a cache entry and cookie.
//! 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_session::{storage::RedisActorSessionStore, Session, SessionMiddleware};
use actix_session::{storage::RedisSessionStore, Session, SessionMiddleware};
use actix_web::{
middleware, web,
web::{get, post, resource},
@ -81,16 +81,12 @@ async fn main() -> std::io::Result<()> {
// authentication cookies for any user!
let private_key = actix_web::cookie::Key::generate();
let store = RedisSessionStore::new("127.0.0.1:6379").await.unwrap();
HttpServer::new(move || {
App::new()
// redis session middleware
.wrap(
SessionMiddleware::builder(
RedisActorSessionStore::new("127.0.0.1:6379"),
private_key.clone(),
)
.build(),
)
.wrap(SessionMiddleware::builder(store.clone(), private_key.clone()).build())
// enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index)))
@ -112,15 +108,13 @@ mod test {
#[actix_web::test]
async fn test_workflow() {
let private_key = actix_web::cookie::Key::generate();
let store = RedisSessionStore::new("127.0.0.1:6379").await.unwrap();
let srv = actix_test::start(move || {
App::new()
.wrap(
SessionMiddleware::builder(
RedisActorSessionStore::new("127.0.0.1:6379"),
private_key.clone(),
)
.cookie_name("test-session".to_owned())
.build(),
SessionMiddleware::builder(store.clone(), private_key.clone())
.cookie_name("test-session".to_owned())
.build(),
)
.wrap(middleware::Logger::default())
.service(resource("/").route(get().to(index)))