Sessions for Rocket.rs
https://crates.io/crates/rocket_session
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
663 B
29 lines
663 B
2 years ago
|
#[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)
|
||
|
}
|