2019-06-17 22:57:57 +02:00
|
|
|
// <user-session>
|
|
|
|
use actix_session::{CookieSession, Session};
|
2019-06-26 08:59:20 +02:00
|
|
|
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
2019-06-17 22:57:57 +02:00
|
|
|
|
2019-06-26 08:59:20 +02:00
|
|
|
pub fn index(session: Session) -> Result<HttpResponse, Error> {
|
|
|
|
// access session data
|
2019-06-17 22:57:57 +02:00
|
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
2019-06-26 08:59:20 +02:00
|
|
|
session.set("counter", count + 1)?;
|
2019-06-17 22:57:57 +02:00
|
|
|
} else {
|
2019-06-26 08:59:20 +02:00
|
|
|
session.set("counter", 1)?;
|
2019-06-17 22:57:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 08:59:20 +02:00
|
|
|
Ok(HttpResponse::Ok().body(format!(
|
|
|
|
"Count is {:?}!",
|
|
|
|
session.get::<i32>("counter")?.unwrap()
|
|
|
|
)))
|
2019-06-17 22:57:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 08:59:20 +02:00
|
|
|
pub fn main() {
|
2019-06-17 22:57:57 +02:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2019-06-26 08:59:20 +02:00
|
|
|
.wrap(
|
|
|
|
CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
|
|
|
|
.secure(false),
|
|
|
|
)
|
2019-06-17 22:57:57 +02:00
|
|
|
.service(web::resource("/").to(index))
|
|
|
|
})
|
2019-06-26 08:59:20 +02:00
|
|
|
.bind("127.0.0.1:8088")
|
|
|
|
.unwrap()
|
2019-06-17 22:57:57 +02:00
|
|
|
.run()
|
2019-06-26 08:59:20 +02:00
|
|
|
.unwrap();
|
2019-06-17 22:57:57 +02:00
|
|
|
}
|
|
|
|
// </user-session>
|