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)
|
2022-03-01 05:12:20 +01:00
|
|
|
[![Documentation](https://docs.rs/actix-redis/badge.svg?version=0.10.0)](https://docs.rs/actix-redis/0.10.0)
|
2021-03-21 10:38:29 +01:00
|
|
|
![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-redis)
|
2022-03-01 05:12:20 +01:00
|
|
|
[![Dependency Status](https://deps.rs/crate/actix-redis/0.10.0/status.svg)](https://deps.rs/crate/actix-redis/0.10.0)
|
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-04-09 15:39:50 +02:00
|
|
|
- [API Documentation](https://docs.rs/actix-redis)
|
2022-02-18 04:33:03 +01:00
|
|
|
- [Example Project](https://github.com/actix/examples/tree/master/auth/redis-session)
|
2021-12-29 10:42:31 +01:00
|
|
|
- Minimum Supported Rust Version (MSRV): 1.54
|
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
|
2021-04-09 15:39:50 +02:00
|
|
|
use actix_web::{App, HttpServer, middleware::Logger};
|
|
|
|
use actix_web::web::{resource, get}
|
2020-05-15 13:14:43 +02:00
|
|
|
use actix_redis::RedisSession;
|
2017-12-29 10:10:27 +01:00
|
|
|
|
2021-04-09 15:39:50 +02:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
HttpServer::new(move || App::new()
|
2019-12-20 17:11:38 +01:00
|
|
|
// cookie session middleware
|
2021-04-09 15:39:50 +02:00
|
|
|
.wrap(RedisSession::new("127.0.0.1:6379", &[0; 32]))
|
|
|
|
// enable logger
|
|
|
|
.wrap(Logger::default())
|
2019-12-20 17:11:38 +01:00
|
|
|
// register simple route, handle all methods
|
2021-04-09 15:39:50 +02:00
|
|
|
.service(resource("/").route(get().to(index)))
|
2019-12-20 17:11:38 +01:00
|
|
|
)
|
2021-04-09 15:39:50 +02:00
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
2019-12-20 17:11:38 +01:00
|
|
|
.await
|
2017-12-29 10:10:27 +01:00
|
|
|
}
|
|
|
|
```
|