-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharound.rs
67 lines (54 loc) · 1.87 KB
/
around.rs
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
extern crate iron;
extern crate time;
use iron::prelude::*;
use iron::{Handler, AroundMiddleware};
use iron::status;
enum LoggerMode {
Silent,
Tiny,
Large
}
struct Logger {
mode: LoggerMode
}
struct LoggerHandler<H: Handler> { logger: Logger, handler: H }
impl Logger {
fn new(mode: LoggerMode) -> Logger {
Logger { mode: mode }
}
fn log(&self, req: &Request, res: Result<&Response, &IronError>, time: u64) {
match self.mode {
LoggerMode::Silent => {},
LoggerMode::Tiny => println!("Req: {:?}\nRes: {:?}\nTook: {}", req, res, time),
LoggerMode::Large => println!("Request: {:?}\nResponse: {:?}\nResponse-Time: {}", req, res, time)
}
}
}
impl<H: Handler> Handler for LoggerHandler<H> {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let entry = ::time::precise_time_ns();
let res = self.handler.handle(req);
self.logger.log(req, res.as_ref(), ::time::precise_time_ns() - entry);
res
}
}
impl AroundMiddleware for Logger {
fn around(self, handler: Box<Handler>) -> Box<Handler> {
Box::new(LoggerHandler {
logger: self,
handler: handler
}) as Box<Handler>
}
}
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
fn main() {
let tiny = Iron::new(Logger::new(LoggerMode::Tiny).around(Box::new(hello_world)));
let silent = Iron::new(Logger::new(LoggerMode::Silent).around(Box::new(hello_world)));
let large = Iron::new(Logger::new(LoggerMode::Large).around(Box::new(hello_world)));
let _tiny_listening = tiny.http("localhost:2000").unwrap();
let _silent_listening = silent.http("localhost:3000").unwrap();
let _large_listening = large.http("localhost:4000").unwrap();
println!("Servers listening on 2000, 3000, and 4000");
}