Skip to content

Instantly share code, notes, and snippets.

@ELD
Created December 26, 2020 21:49
Show Gist options
  • Save ELD/e51889ba6133ac8314544b19cbb04a1b to your computer and use it in GitHub Desktop.
Save ELD/e51889ba6133ac8314544b19cbb04a1b to your computer and use it in GitHub Desktop.
use rocket::{error::Error, get, response::content::Html, routes};
use turbopump::nightly::{
fairing::{config::SameSite, config::SessionConfig, SessionFairing},
in_memory::InMemory,
Session,
};
#[derive(Clone, Default)]
struct SessionData {
hit_counter: u32,
}
#[rocket::main]
async fn main() -> Result<(), Error> {
rocket::ignite()
.attach(
SessionFairing::<SessionData, InMemory<SessionData>>::with_config(SessionConfig {
max_age: 3600,
domain: None,
path: Some("/".to_string()),
same_site: SameSite::Lax,
http_only: true,
}),
)
.mount("/", routes![hit_counter])
.launch()
.await
}
#[get("/hit-counter")]
async fn hit_counter(session: &Session<SessionData>) -> Html<String> {
let count = session.tap(|data| {
data.hit_counter += 1;
data.hit_counter
});
Html(format!(
r##"
<h1>Hello, world!</h1>
<p>You've visited this page {} times</p>"##,
count
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment