2019-06-17 22:57:57 +02:00
|
|
|
// <user-session>
|
|
|
|
use actix_session::{CookieSession, Session};
|
|
|
|
use actix_web::{middleware::Logger, web, App, HttpRequest, HttpServer, Result};
|
|
|
|
|
|
|
|
/// simple index handler with session
|
|
|
|
fn index(session: Session, req: HttpRequest) -> Result<&'static str> {
|
|
|
|
println!("{:?}", req);
|
|
|
|
|
|
|
|
// RequestSession trait is used for session access
|
|
|
|
let mut counter = 1;
|
|
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
|
|
|
println!("SESSION value: {}", count);
|
|
|
|
counter = count + 1;
|
|
|
|
session.set("counter", counter)?;
|
|
|
|
} else {
|
|
|
|
session.set("counter", counter)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok("welcome!")
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:20:50 +02:00
|
|
|
pub fn main() -> std::io::Result<()> {
|
2019-06-17 22:57:57 +02:00
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
// enable logger
|
|
|
|
.wrap(Logger::default())
|
|
|
|
// cookie session middleware
|
|
|
|
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
|
|
|
.service(web::resource("/").to(index))
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
|
|
|
}
|
|
|
|
// </user-session>
|