From d195aace48bb2b183686c5cdb827830f3edd1729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 31 Dec 2019 02:26:36 +0100 Subject: [PATCH] add a readme --- README.md | 33 +++++++++++++++++++++++++++++++++ src/session.rs | 5 +---- 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b7a343 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Sessions for Rocket.rs + +Adding cookie-based sessions to a rocket application is extremely simple: + +```rust +#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; + +use rocket_session::Session; +use std::time::Duration; + +fn main() { + rocket::ignite() + .attach(Session::fairing(Duration::from_secs(3600))) + .mount("/", routes![index]) + .launch(); +} + +#[get("/")] +fn index(session: Session) -> String { + let mut count: usize = session.get_or_default("count"); + count += 1; + session.set("count", count); + + format!("{} visits", count) +} +``` + +Anything serializable can be stored in the session, just make sure to unpack it to the right type. + +The session driver internally uses `serde_json::Value` and the `json_dotpath` crate. +Therefore, it's possible to use dotted paths and store the session data in a more structured way. + diff --git a/src/session.rs b/src/session.rs index 1adec2a..71d3198 100644 --- a/src/session.rs +++ b/src/session.rs @@ -40,12 +40,9 @@ impl<'a, 'r> FromRequest<'a, 'r> for &'a SessionID { fn from_request(request: &'a Request<'r>) -> Outcome { Outcome::Success(request.local_cache(|| { - println!("get id"); if let Some(cookie) = request.cookies().get(SESSION_ID) { - println!("from cookie"); SessionID(cookie.value().to_string()) // FIXME avoid cloning (cow?) } else { - println!("new id"); SessionID( rand::thread_rng() .sample_iter(&rand::distributions::Alphanumeric) @@ -158,7 +155,7 @@ struct SessionFairing { impl Fairing for SessionFairing { fn info(&self) -> Info { Info { - name: "Session Fairing", + name: "Session", kind: fairing::Kind::Attach | fairing::Kind::Response, } }