1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00
actix-extras/examples/basic.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

2017-12-29 06:14:04 +01:00
#![allow(unused_variables)]
2018-05-08 19:12:57 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
2017-12-29 06:14:04 +01:00
extern crate actix;
extern crate actix_redis;
2018-05-08 19:12:57 +02:00
extern crate actix_web;
2017-12-29 06:14:04 +01:00
extern crate env_logger;
extern crate futures;
use actix_redis::RedisSessionBackend;
2018-05-08 19:48:23 +02:00
use actix_web::middleware::session::{self, RequestSession};
2018-05-08 19:12:57 +02:00
use actix_web::{middleware, server, App, HttpRequest, HttpResponse, Result};
2017-12-29 06:14:04 +01:00
/// simple handler
2018-07-20 23:53:56 +02:00
fn index(req: &HttpRequest) -> Result<HttpResponse> {
2017-12-29 06:14:04 +01:00
println!("{:?}", req);
// session
if let Some(count) = req.session().get::<i32>("counter")? {
println!("SESSION value: {}", count);
2018-05-08 19:12:57 +02:00
req.session().set("counter", count + 1)?;
2017-12-29 06:14:04 +01:00
} else {
req.session().set("counter", 1)?;
}
Ok("Welcome!".into())
}
fn main() {
2018-01-22 09:40:50 +01:00
::std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info");
env_logger::init();
2017-12-29 06:14:04 +01:00
let sys = actix::System::new("basic-example");
2018-05-08 19:12:57 +02:00
server::new(|| {
App::new()
2017-12-29 06:14:04 +01:00
// enable logger
.middleware(middleware::Logger::default())
// cookie session middleware
2018-05-08 19:48:23 +02:00
.middleware(session::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
))
// register simple route, handle all methods
2018-05-08 19:12:57 +02:00
.resource("/", |r| r.f(index))
}).bind("0.0.0.0:8080")
.unwrap()
2018-05-08 19:48:23 +02:00
.workers(1)
2017-12-29 06:14:04 +01:00
.start();
let _ = sys.run();
}