mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
45 lines
1.3 KiB
Markdown
45 lines
1.3 KiB
Markdown
# Actix redis
|
|
|
|
## Redis session backend
|
|
|
|
Use redis as session storage.
|
|
|
|
You need to pass an address of the redis server and random value to the
|
|
constructor of `RedisSessionBackend`. This is private key for cookie session,
|
|
When this value is changed, all session data is lost.
|
|
|
|
Note that whatever you write into your session is visible by the user (but not modifiable).
|
|
|
|
Constructor panics if key length is less than 32 bytes.
|
|
|
|
```rust,ignore
|
|
extern crate actix_web;
|
|
extern crate actix_redis;
|
|
|
|
use actix_web::*;
|
|
use actix_web::middleware::SessionStorage;
|
|
use actix_redis::RedisSessionBackend;
|
|
|
|
fn main() {
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
let _ = env_logger::init();
|
|
let sys = actix::System::new("basic-example");
|
|
|
|
HttpServer::new(
|
|
|| Application::new()
|
|
// enable logger
|
|
.middleware(middleware::Logger::default())
|
|
// cookie session middleware
|
|
.middleware(SessionStorage::new(
|
|
RedisSessionBackend::new("127.0.0.1:6379", &[0; 32])
|
|
.expect("Can not connect to redis server")
|
|
))
|
|
// register simple route, handle all methods
|
|
.resource("/", |r| r.f(index)))
|
|
.bind("0.0.0.0:8080").unwrap()
|
|
.start();
|
|
|
|
let _ = sys.run();
|
|
}
|
|
```
|