forked from packages/rocket_session
parent
af30c552ac
commit
cde08fe788
@ -0,0 +1,70 @@ |
||||
#![feature(proc_macro_hygiene, decl_macro)] |
||||
#[macro_use] |
||||
extern crate rocket; |
||||
|
||||
use rocket::response::content::Html; |
||||
use rocket::response::Redirect; |
||||
use rocket::request::Form; |
||||
|
||||
type Session<'a> = rocket_session::Session<'a, Vec<String>>; |
||||
|
||||
fn main() { |
||||
rocket::ignite() |
||||
.attach(Session::fairing()) |
||||
.mount("/", routes![index, add, remove]) |
||||
.launch(); |
||||
} |
||||
|
||||
#[get("/")] |
||||
fn index(session: Session) -> Html<String> { |
||||
let mut page = String::new(); |
||||
page.push_str(r#" |
||||
<!DOCTYPE html> |
||||
<h1>My Dogs</h1> |
||||
|
||||
<form method="POST" action="/add"> |
||||
Add Dog: <input type="text" name="name"> <input type="submit" value="Add"> |
||||
</form> |
||||
|
||||
<ul> |
||||
"#); |
||||
|
||||
session.tap(|sess| { |
||||
for (n, dog) in sess.iter().enumerate() { |
||||
page.push_str(&format!(r#" |
||||
<li>🐶 {} <a href="/remove/{}">Remove</a></li> |
||||
"#, dog, n)); |
||||
} |
||||
}); |
||||
|
||||
page.push_str(r#" |
||||
</ul> |
||||
"#); |
||||
|
||||
Html(page) |
||||
} |
||||
|
||||
#[derive(FromForm)] |
||||
struct AddForm { |
||||
name: String, |
||||
} |
||||
|
||||
#[post("/add", data="<dog>")] |
||||
fn add(session: Session, dog : Form<AddForm>) -> Redirect { |
||||
session.tap(move |sess| { |
||||
sess.push(dog.into_inner().name); |
||||
}); |
||||
|
||||
Redirect::found("/") |
||||
} |
||||
|
||||
#[get("/remove/<dog>")] |
||||
fn remove(session: Session, dog : usize) -> Redirect { |
||||
session.tap(|sess| { |
||||
if dog < sess.len() { |
||||
sess.remove(dog); |
||||
} |
||||
}); |
||||
|
||||
Redirect::found("/") |
||||
} |
@ -0,0 +1,72 @@ |
||||
//! This demo is a page visit counter, with a custom cookie name, length, and expiry time.
|
||||
//!
|
||||
//! The expiry time is set to 10 seconds to illustrate how a session is cleared if inactive.
|
||||
|
||||
#![feature(proc_macro_hygiene, decl_macro)] |
||||
#[macro_use] |
||||
extern crate rocket; |
||||
|
||||
use std::time::Duration; |
||||
use rocket::response::content::Html; |
||||
|
||||
#[derive(Default, Clone)] |
||||
struct SessionData { |
||||
visits1: usize, |
||||
visits2: usize, |
||||
} |
||||
|
||||
// It's convenient to define a type alias:
|
||||
type Session<'a> = rocket_session::Session<'a, SessionData>; |
||||
|
||||
fn main() { |
||||
rocket::ignite() |
||||
.attach(Session::fairing() |
||||
// 10 seconds of inactivity until session expires
|
||||
// (wait 10s and refresh, the numbers will reset)
|
||||
.with_lifetime(Duration::from_secs(10)) |
||||
// custom cookie name and length
|
||||
.with_cookie_name("my_cookie") |
||||
.with_cookie_len(20) |
||||
) |
||||
.mount("/", routes![index, about]) |
||||
.launch(); |
||||
} |
||||
|
||||
#[get("/")] |
||||
fn index(session: Session) -> Html<String> { |
||||
// Here we build the entire response inside the 'tap' closure.
|
||||
|
||||
// While inside, the session is locked to parallel changes, e.g.
|
||||
// from a different browser tab.
|
||||
session.tap(|sess| { |
||||
sess.visits1 += 1; |
||||
|
||||
Html(format!(r##" |
||||
<!DOCTYPE html> |
||||
<h1>Home</h1> |
||||
<a href="/">Refresh</a> • <a href="/about/">go to About</a> |
||||
<p>Visits: home {}, about {}</p> |
||||
"##, |
||||
sess.visits1, |
||||
sess.visits2 |
||||
)) |
||||
}) |
||||
} |
||||
|
||||
#[get("/about")] |
||||
fn about(session: Session) -> Html<String> { |
||||
// Here we return a value from the tap function and use it below
|
||||
let count = session.tap(|sess| { |
||||
sess.visits2 += 1; |
||||
sess.visits2 |
||||
}); |
||||
|
||||
Html(format!(r##" |
||||
<!DOCTYPE html> |
||||
<h1>About</h1> |
||||
<a href="/about">Refresh</a> • <a href="/">go home</a> |
||||
<p>Page visits: {}</p> |
||||
"##, |
||||
count |
||||
)) |
||||
} |
Loading…
Reference in new issue