2017-12-29 06:14:04 +01:00
|
|
|
#![allow(unused_variables)]
|
|
|
|
#![cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
|
|
|
|
|
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate actix_redis;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate futures;
|
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use actix_web::middleware::RequestSession;
|
|
|
|
use actix_redis::RedisSessionBackend;
|
|
|
|
|
|
|
|
|
|
|
|
/// simple handler
|
|
|
|
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
// session
|
|
|
|
if let Some(count) = req.session().get::<i32>("counter")? {
|
|
|
|
println!("SESSION value: {}", count);
|
|
|
|
req.session().set("counter", count+1)?;
|
|
|
|
} else {
|
|
|
|
req.session().set("counter", 1)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok("Welcome!".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
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(middleware::SessionStorage::new(
|
2017-12-29 10:10:27 +01:00
|
|
|
RedisSessionBackend::new("127.0.0.1:6379", &[0; 32])
|
2017-12-29 06:14:04 +01:00
|
|
|
.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()
|
2017-12-29 10:10:27 +01:00
|
|
|
.threads(1)
|
2017-12-29 06:14:04 +01:00
|
|
|
.start();
|
|
|
|
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|