Crate fe_session [] [src]

Overview

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.

Examples

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::<usize>();
    let mut result  = String::new();

    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::<usize>::new());

let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();

let mut conn1 = FeConnection::new("localhost", 3000);
let mut conn2 = FeConnection::new("localhost", 3000);

assert_eq!(conn1.get_string("/"), "0");
assert_eq!(conn2.get_string("/"), "0");
assert_eq!(conn1.get_string("/"), "1");
assert_eq!(conn1.get_string("/"), "2");
assert_eq!(conn2.get_string("/"), "1");

iron.close().unwrap();

Structs

FeConnection

A HTTP client connection that is cookie aware.

FeSession

This object holds information about the session for the current request.

FeSessionConfig

Configuration object for the iron-session module.

FeSessionStorage

Storage object that holds the mapping of session IDs to sessions.

Enums

FeSessionState

Enumeration to identify the state of a user's session.

Traits

FeSessionRequest

Helper trait that adds methods for accessing a user's session onto the Iron's Request object.