1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-22 23:05:56 +01:00

38 lines
1.0 KiB
Rust
Raw Normal View History

2019-03-29 11:31:48 -07:00
use actix_redis::RedisSession;
use actix_session::Session;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpServer, Responder};
2017-12-28 21:14:04 -08:00
/// simple handler
2019-12-16 00:04:47 +06:00
async fn index(req: HttpRequest, session: Session) -> Result<impl Responder, Error> {
2017-12-28 21:14:04 -08:00
println!("{:?}", req);
// session
2019-03-29 11:31:48 -07:00
if let Some(count) = session.get::<i32>("counter")? {
2017-12-28 21:14:04 -08:00
println!("SESSION value: {}", count);
2019-03-29 11:31:48 -07:00
session.set("counter", count + 1)?;
2017-12-28 21:14:04 -08:00
} else {
2019-03-29 11:31:48 -07:00
session.set("counter", 1)?;
2017-12-28 21:14:04 -08:00
}
2019-03-29 11:31:48 -07:00
Ok("Welcome!")
2017-12-28 21:14:04 -08:00
}
2019-12-16 00:04:47 +06:00
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
2019-03-29 11:31:48 -07:00
std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info");
2018-01-22 00:40:50 -08:00
env_logger::init();
2017-12-28 21:14:04 -08:00
2019-03-29 11:31:48 -07:00
HttpServer::new(|| {
2018-05-08 10:12:57 -07:00
App::new()
2017-12-28 21:14:04 -08:00
// enable logger
2019-03-29 11:31:48 -07:00
.wrap(middleware::Logger::default())
2017-12-28 21:14:04 -08:00
// cookie session middleware
2019-03-29 11:31:48 -07:00
.wrap(RedisSession::new("127.0.0.1:6379", &[0; 32]))
2017-12-28 21:14:04 -08:00
// register simple route, handle all methods
2019-03-29 11:31:48 -07:00
.service(web::resource("/").to(index))
})
.bind("0.0.0.0:8080")?
2019-12-26 15:47:31 +09:00
.run()
2019-12-16 00:04:47 +06:00
.await
2017-12-28 21:14:04 -08:00
}