1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

restructure folders

This commit is contained in:
Rob Ede
2022-02-18 02:01:48 +00:00
parent 4d8573c3fe
commit cc3d356209
201 changed files with 52 additions and 49 deletions

View File

@ -0,0 +1,10 @@
[package]
name = "cookie-session"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4.0.0-beta.21"
actix-session = "0.5.0-beta.7"
log = "0.4"
env_logger = "0.9.0"

View File

@ -0,0 +1,7 @@
## Cookie session example
```sh
cd session/cookie-session
cargo run
# Starting http server: 127.0.0.1:8080
```

View File

@ -0,0 +1,45 @@
//! Example of cookie based session
//! Session data is stored in cookie, it is limited to 4kb
//!
//! [Redis session example](https://github.com/actix/examples/tree/master/redis-session)
//!
//! [User guide](https://actix.rs/docs/middleware/#user-sessions)
use actix_session::{CookieSession, Session};
use actix_web::{middleware::Logger, web, App, HttpRequest, HttpServer, Result};
/// simple index handler with session
async fn index(session: Session, req: HttpRequest) -> Result<&'static str> {
log::info!("{:?}", req);
// RequestSession trait is used for session access
let mut counter = 1;
if let Some(count) = session.get::<i32>("counter")? {
log::info!("SESSION value: {}", count);
counter = count + 1;
session.insert("counter", counter)?;
} else {
session.insert("counter", counter)?;
}
Ok("welcome!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
log::info!("Starting http server: 127.0.0.1:8080");
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()
.await
}