mirror of
https://github.com/actix/actix-website
synced 2025-03-15 04:33:07 +01:00
36 lines
918 B
Rust
36 lines
918 B
Rust
#![allow(dead_code)]
|
|
|
|
// <user-session>
|
|
use actix_session::{CookieSession, Session};
|
|
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
|
|
|
async fn index(session: Session) -> Result<HttpResponse, Error> {
|
|
// access session data
|
|
if let Some(count) = session.get::<i32>("counter")? {
|
|
session.set("counter", count + 1)?;
|
|
} else {
|
|
session.set("counter", 1)?;
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().body(format!(
|
|
"Count is {:?}!",
|
|
session.get::<i32>("counter")?.unwrap()
|
|
)))
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
.wrap(
|
|
CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
|
|
.secure(false),
|
|
)
|
|
.service(web::resource("/").to(index))
|
|
})
|
|
.bind("127.0.0.1:8080")?
|
|
.run()
|
|
.await
|
|
}
|
|
// </user-session>
|