mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
36 lines
1023 B
Rust
36 lines
1023 B
Rust
use actix_redis::RedisSession;
|
|
use actix_session::Session;
|
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpServer, Responder};
|
|
|
|
/// simple handler
|
|
fn index(req: HttpRequest, session: Session) -> Result<impl Responder, Error> {
|
|
println!("{:?}", req);
|
|
|
|
// session
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
|
println!("SESSION value: {}", count);
|
|
session.set("counter", count + 1)?;
|
|
} else {
|
|
session.set("counter", 1)?;
|
|
}
|
|
|
|
Ok("Welcome!")
|
|
}
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info");
|
|
env_logger::init();
|
|
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
// enable logger
|
|
.wrap(middleware::Logger::default())
|
|
// cookie session middleware
|
|
.wrap(RedisSession::new("127.0.0.1:6379", &[0; 32]))
|
|
// register simple route, handle all methods
|
|
.service(web::resource("/").to(index))
|
|
})
|
|
.bind("0.0.0.0:8080")?
|
|
.run()
|
|
}
|