2020-01-30 01:31:25 +01:00
|
|
|
# actix-redis
|
2018-01-23 08:16:24 +01:00
|
|
|
|
2021-03-21 10:38:29 +01:00
|
|
|
> Redis integration for Actix and session store for Actix Web.
|
2020-01-30 01:31:25 +01:00
|
|
|
|
2021-03-21 10:38:29 +01:00
|
|
|
[![crates.io](https://img.shields.io/crates/v/actix-redis?label=latest)](https://crates.io/crates/actix-redis)
|
|
|
|
[![Documentation](https://docs.rs/actix-redis/badge.svg?version=0.9.2)](https://docs.rs/actix-redis/0.9.2)
|
|
|
|
![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-redis)
|
|
|
|
[![Dependency Status](https://deps.rs/crate/actix-redis/0.9.2/status.svg)](https://deps.rs/crate/actix-redis/0.9.2)
|
2018-01-23 08:13:18 +01:00
|
|
|
|
2021-03-21 10:38:29 +01:00
|
|
|
## Documentation & Resources
|
2018-01-23 08:13:18 +01:00
|
|
|
|
2021-03-21 10:38:29 +01:00
|
|
|
- [API Documentation](https://docs.rs/actix-cors)
|
|
|
|
- [Example Project](https://github.com/actix/examples/tree/HEAD/session/redis-session)
|
2021-04-02 12:31:30 +02:00
|
|
|
- Minimum Supported Rust Version (MSRV): 1.46
|
2018-01-23 08:13:18 +01:00
|
|
|
|
2021-03-21 10:38:29 +01:00
|
|
|
## Redis Session Backend
|
2017-12-29 10:10:27 +01:00
|
|
|
|
|
|
|
Use redis as session storage.
|
|
|
|
|
|
|
|
You need to pass an address of the redis server and random value to the
|
2020-05-15 13:14:43 +02:00
|
|
|
constructor of `RedisSession`. This is private key for cookie session,
|
2017-12-29 10:10:27 +01:00
|
|
|
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.
|
|
|
|
|
2018-05-08 19:12:57 +02:00
|
|
|
```rust
|
2019-12-20 17:11:38 +01:00
|
|
|
use actix_web::{App, HttpServer, web, middleware};
|
2018-05-08 19:12:57 +02:00
|
|
|
use actix_web::middleware::session::SessionStorage;
|
2020-05-15 13:14:43 +02:00
|
|
|
use actix_redis::RedisSession;
|
2017-12-29 10:10:27 +01:00
|
|
|
|
2019-12-20 17:11:38 +01:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result {
|
|
|
|
HttpServer::new(|| App::new()
|
|
|
|
// enable logger
|
|
|
|
.middleware(middleware::Logger::default())
|
|
|
|
// cookie session middleware
|
|
|
|
.middleware(SessionStorage::new(
|
2020-05-15 13:14:43 +02:00
|
|
|
RedisSession::new("127.0.0.1:6379", &[0; 32])
|
2019-12-20 17:11:38 +01:00
|
|
|
))
|
|
|
|
// register simple route, handle all methods
|
|
|
|
.service(web::resource("/").to(index))
|
|
|
|
)
|
|
|
|
.bind("0.0.0.0:8080")?
|
|
|
|
.start()
|
|
|
|
.await
|
2017-12-29 10:10:27 +01:00
|
|
|
}
|
|
|
|
```
|