mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
add cookie session example
This commit is contained in:
parent
0b9d7ab855
commit
85af2bce54
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -347,6 +347,17 @@ dependencies = [
|
||||
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cookie-session"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.1.2"
|
||||
|
@ -3,6 +3,7 @@ members = [
|
||||
"./",
|
||||
"basics",
|
||||
"cookie-auth",
|
||||
"cookie-session",
|
||||
"diesel",
|
||||
"hello-world",
|
||||
"http-proxy",
|
||||
|
12
cookie-session/Cargo.toml
Normal file
12
cookie-session/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "cookie-session"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.5"
|
||||
actix-web = "^0.5"
|
||||
futures = "0.1"
|
||||
time = "0.1"
|
||||
env_logger = "0.5"
|
54
cookie-session/src/main.rs
Normal file
54
cookie-session/src/main.rs
Normal file
@ -0,0 +1,54 @@
|
||||
//! 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/book/actix-web/sec-9-middlewares.html#user-sessions)
|
||||
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
|
||||
use actix_web::middleware::{self, RequestSession};
|
||||
use actix_web::{server, App, HttpRequest, Result};
|
||||
use std::env;
|
||||
|
||||
/// simple index handler with session
|
||||
fn index(mut req: HttpRequest) -> Result<&'static str> {
|
||||
println!("{:?}", req);
|
||||
|
||||
// RequestSession trait is used for session access
|
||||
let mut counter = 1;
|
||||
if let Some(count) = req.session().get::<i32>("counter")? {
|
||||
println!("SESSION value: {}", count);
|
||||
counter = count + 1;
|
||||
req.session().set("counter", counter)?;
|
||||
} else {
|
||||
req.session().set("counter", counter)?;
|
||||
}
|
||||
|
||||
Ok("welcome!")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
let sys = actix::System::new("session-example");
|
||||
|
||||
server::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
// cookie session middleware
|
||||
.middleware(middleware::SessionStorage::new(
|
||||
middleware::CookieSessionBackend::signed(&[0; 32]).secure(false)
|
||||
))
|
||||
.resource("/", |r| r.f(index))
|
||||
}).bind("127.0.0.1:8080")
|
||||
.expect("Can not bind to 127.0.0.1:8080")
|
||||
.start();
|
||||
|
||||
println!("Starting http server: 127.0.0.1:8080");
|
||||
let _ = sys.run();
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
//! Example of redis based session
|
||||
//!
|
||||
//! [User guide](https://actix.rs/book/actix-web/sec-9-middlewares.html#user-sessions)
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate actix_redis;
|
||||
extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
|
||||
use actix_web::{server, App, HttpRequest, HttpResponse, Result};
|
||||
use actix_web::middleware::{Logger, SessionStorage, RequestSession};
|
||||
use actix_redis::RedisSessionBackend;
|
||||
|
||||
use actix_web::middleware::{Logger, RequestSession, SessionStorage};
|
||||
use actix_web::{server, App, HttpRequest, HttpResponse, Result};
|
||||
|
||||
/// simple handler
|
||||
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||
@ -17,7 +17,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||
// session
|
||||
if let Some(count) = req.session().get::<i32>("counter")? {
|
||||
println!("SESSION value: {}", count);
|
||||
req.session().set("counter", count+1)?;
|
||||
req.session().set("counter", count + 1)?;
|
||||
} else {
|
||||
req.session().set("counter", 1)?;
|
||||
}
|
||||
@ -30,18 +30,18 @@ fn main() {
|
||||
env_logger::init();
|
||||
let sys = actix::System::new("basic-example");
|
||||
|
||||
server::new(
|
||||
|| App::new()
|
||||
server::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(Logger::default())
|
||||
// cookie session middleware
|
||||
// redis session middleware
|
||||
.middleware(SessionStorage::new(
|
||||
RedisSessionBackend::new("127.0.0.1:6379", &[0; 32])
|
||||
))
|
||||
// register simple route, handle all methods
|
||||
.resource("/", |r| r.f(index)))
|
||||
.bind("0.0.0.0:8080").unwrap()
|
||||
.threads(1)
|
||||
.resource("/", |r| r.f(index))
|
||||
}).bind("0.0.0.0:8080")
|
||||
.unwrap()
|
||||
.start();
|
||||
|
||||
let _ = sys.run();
|
||||
|
Loading…
Reference in New Issue
Block a user