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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#![doc(html_root_url = "https://marad.github.io/hyper-router/doc/hyper_router")]
extern crate futures;
extern crate hyper;
use futures::future::FutureResult;
use hyper::header::ContentLength;
use hyper::server::{Service, Request, Response};
use hyper::StatusCode;
use hyper::Method;
mod path;
pub mod route;
mod builder;
pub mod handlers;
pub use self::path::Path;
pub use self::route::Route;
pub use self::route::RouteBuilder;
pub use self::builder::RouterBuilder;
pub type Handler = fn(Request) -> Response;
pub type HttpResult<T> = Result<T,StatusCode>;
#[derive(Debug)]
pub struct Router {
routes: Vec<Route>
}
impl Router {
pub fn find_handler_with_defaults(&self, request: &Request) -> Handler {
let matching_routes = self.find_matching_routes(request.path());
match matching_routes.len() {
x if x <= 0 => handlers::default_404_handler,
_ => {
self.find_for_method(&matching_routes, request.method())
.unwrap_or(handlers::method_not_supported_handler)
}
}
}
pub fn find_handler(&self, request: &Request) -> HttpResult<Handler> {
let matching_routes = self.find_matching_routes(request.path());
match matching_routes.len() {
x if x <= 0 => Err(StatusCode::NotFound),
_ => {
self.find_for_method(&matching_routes, request.method())
.map(|handler| Ok(handler))
.unwrap_or(Err(StatusCode::MethodNotAllowed))
}
}
}
pub fn find_matching_routes(&self, request_path: &str) -> Vec<&Route> {
self.routes.iter()
.filter(|route| {
route.path.matcher.is_match(&request_path)
})
.collect()
}
fn find_for_method(&self, routes: &Vec<&Route>, method: &Method) -> Option<Handler> {
let method = method.clone();
routes.iter()
.find(|route| route.method == method)
.map(|route| route.handler)
}
}
#[derive(Debug)]
pub struct RouterService {
pub router: Router,
pub error_handler: fn(StatusCode) -> Response
}
impl RouterService {
pub fn new(router: Router) -> RouterService {
RouterService {
router,
error_handler: Self::default_error_handler
}
}
fn default_error_handler(status_code: StatusCode) -> Response {
let error = "Routing error: page not found";
let response = Response::new()
.with_header(ContentLength(error.len() as u64))
.with_body(error);
match status_code {
StatusCode::NotFound => response.with_status(StatusCode::NotFound),
_ => response.with_status(StatusCode::InternalServerError)
}
}
}
impl Service for RouterService {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = FutureResult<Response, hyper::Error>;
fn call(&self, request: Request) -> Self::Future {
futures::future::ok(
match self.router.find_handler(&request) {
Ok(handler) => handler(request),
Err(status_code) => (self.error_handler)(status_code)
}
)
}
}