extern crate iron; extern crate time; use iron::prelude::*; use iron::{BeforeMiddleware, AfterMiddleware, typemap}; use time::precise_time_ns; struct ResponseTime; impl typemap::Key for ResponseTime { type Value = u64; } impl BeforeMiddleware for ResponseTime { fn before(&self, req: &mut Request) -> IronResult<()> { req.extensions.insert::(precise_time_ns()); Ok(()) } } impl AfterMiddleware for ResponseTime { fn after(&self, req: &mut Request, res: Response) -> IronResult { let delta = precise_time_ns() - *req.extensions.get::().unwrap(); println!("Request took: {} ms", (delta as f64) / 1000000.0); Ok(res) } } fn hello_world(_: &mut Request) -> IronResult { Ok(Response::with((iron::status::Ok, "Hello World"))) } fn main() { let mut chain = Chain::new(hello_world); chain.link_before(ResponseTime); chain.link_after(ResponseTime); Iron::new(chain).http("localhost:3000").unwrap(); }