1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright 2016 Joshua Gentry
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */
#![warn(missing_docs)]

//! # 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
//!
//! ```
//! # extern crate iron;
//! # extern crate fe_session;
//! 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};
//!
//! # fn main() {
//! 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();
//! # }
//! ```
extern crate hyper;
extern crate iron;
#[macro_use]
extern crate log;
extern crate rand;
extern crate time;
extern crate cookie;

mod fe_connection;
mod fe_session_config;
mod fe_session_data;
mod fe_session_error;
mod fe_session_request;
mod fe_session_state;
mod fe_session_storage;
mod fe_session_thread;
mod fe_session;

pub use fe_connection::FeConnection;
pub use fe_session_config::FeSessionConfig;
pub use fe_session_request::FeSessionRequest;
pub use fe_session_state::FeSessionState;
pub use fe_session_storage::FeSessionStorage;
pub use fe_session::FeSession;