This crate provides a session object for applications developed in Iron. This session object is an object implemented by your application and is associated with a single HTTP client. All requests from this HTTP client will have access to this object. The session object is destroyed after a configurable amount of idle time. So if no requests from a client come in for 30 minutes the session is destroyed and the memory is reclaimed.
``` use std::fmt::Write; use std::sync::{Arc, Mutex}; use iron::{Iron, Chain, Request, Response}; use iron::status::Status; use fe_session::{FeSessionStorage, FeSession, FeConnection, FeSessionRequest, FeSessionState};
let mut chain = Chain::new(|req : &mut Request| {
let mut session = req.get_session::
match session.state() {
FeSessionState::Expired => { // Old session expired, create a new one.
req.create_session(0 as usize);
write!(result, "{}", 0).unwrap();
},
FeSessionState::None => { // No session, create a new one.
req.create_session(0 as usize);
write!(result, "{}", 0).unwrap();
},
FeSessionState::Active => { // Active session, increment the count.
let mut count = session.get();
*count += 1;
write!(result, "{}", *count).unwrap();
}
}
Ok(Response::with((Status::Ok, result)))
});
chain.around(FeSessionStorage::
let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();
let mut conn1 = FeConnection::new("localhost", 3000); let mut conn2 = FeConnection::new("localhost", 3000);
asserteq!(conn1.getstring("/"), "0"); asserteq!(conn2.getstring("/"), "0"); asserteq!(conn1.getstring("/"), "1"); asserteq!(conn1.getstring("/"), "2"); asserteq!(conn2.getstring("/"), "1");
iron.close().unwrap(); ```