iron_session 0.0.4

Session middleware for the iron framework.
docs.rs failed to build iron_session-0.0.4
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

Session middleware for the Iron web framework.

The Sessions struct is used to create the new session middleware.

Examples

Create and link a session:

# extern crate iron;
# extern crate iron_session;
#
# use iron_session::*;
# use iron::prelude::*;
#
# fn handler(req: &mut Request) -> IronResult<Response> {
#    Ok(Response::new())
# }
#
# fn main() {
    // create the chain that the session middleware will be linked in.
    let mut chain = Chain::new(handler);

    // create the session middleware.
    let store: HashSessionStore<TypeMapSession> = HashSessionStore::new();
    chain.around(Sessions::new(String::from("secret").into_bytes(), store));
# }

Access the session data in a handler:

# extern crate iron;
# extern crate iron_session;
#
# use iron::prelude::*;
# use iron::status;
# use iron_session::*;
# use iron::typemap::Key;
// Create a key type that we can associate with a value type.
struct Counter;

// implement the Key trait which associates u64 with Counter
impl Key for Counter { type Value = u64;}

// iron request handler
fn create(req: &mut Request) -> IronResult<Response> {

    // get the TypeMap for our session
    let lock = req.extensions.get::<TypeMapSession>().unwrap();

    // we want to write to the map, so get a write lock on it.
    let mut map = lock.write().unwrap();

    // if there was no counter object in the map, create one
    if let None = map.get::<Counter>() {
        map.insert::<Counter>(0);
    }

    // get a mutable reference to the u64 inside the map
    let mut count = map.get_mut::<Counter>().unwrap();
    *count += 1;

    // create a message with our hitcount and return in in a response
    let message = format!("hit count: {}", count);
    let mut res = Response::new();


    res.set_mut(status::Ok);
    res.set_mut(message);
    Ok(res)
}
# fn main() {}