forked from packages/rocket_session
parent
bc2180d1e4
commit
b0a2d8c910
@ -0,0 +1,28 @@ |
|||||||
|
#[macro_use] |
||||||
|
extern crate rocket; |
||||||
|
|
||||||
|
use std::time::Duration; |
||||||
|
|
||||||
|
type Session<'a> = rocket_session::Session<'a, u64>; |
||||||
|
|
||||||
|
#[launch] |
||||||
|
fn rocket() -> _ { |
||||||
|
// This session expires in 15 seconds as a demonstration of session configuration
|
||||||
|
rocket::build() |
||||||
|
.attach(Session::fairing().with_lifetime(Duration::from_secs(15))) |
||||||
|
.mount("/", routes![index]) |
||||||
|
} |
||||||
|
|
||||||
|
#[get("/")] |
||||||
|
fn index(session: Session) -> String { |
||||||
|
let count = session.tap(|n| { |
||||||
|
// Change the stored value (it is &mut)
|
||||||
|
*n += 1; |
||||||
|
|
||||||
|
// Return something to the caller.
|
||||||
|
// This can be any type, 'tap' is generic.
|
||||||
|
*n |
||||||
|
}); |
||||||
|
|
||||||
|
format!("{} visits", count) |
||||||
|
} |
Loading…
Reference in new issue