#session #response-status #iron #object #request #req

fe_session

Provides local session storage for Iron

4 releases

Uses old Rust 2015

0.5.0 Jul 8, 2016
0.4.2 Apr 24, 2016
0.4.1 Mar 27, 2016
0.4.0 Mar 19, 2016

#466 in HTTP client

AML/Apache-2.0

88KB
1K SLoC

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();

Dependencies

~5.5MB
~125K SLoC