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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use std::net::ToSocketAddrs;
use std::time::Duration;
use std::env;
use std::error::Error as StdError;
use router::{Router, HttpRouter, Matcher};
use middleware::{MiddlewareStack, Middleware, ErrorHandler};
use server::{Server, ListeningServer};
use template_cache::ReloadPolicy;
use hyper::method::Method;
use hyper::net::SslServer;
use hyper::status::StatusCode;
use default_error_handler::DefaultErrorHandler;
pub struct Options {
output_on_listen: bool,
thread_count: Option<usize>,
reload_policy: ReloadPolicy,
}
impl Options {
pub fn output_on_listen(mut self, output: bool) -> Self {
self.output_on_listen = output;
self
}
pub fn thread_count(mut self, thread_count: Option<usize>) -> Self {
self.thread_count = thread_count;
self
}
pub fn reload_policy(mut self, reload_policy: ReloadPolicy) -> Self {
self.reload_policy = reload_policy;
self
}
}
impl Default for Options {
fn default() -> Self {
Options {
output_on_listen: true,
thread_count: None,
reload_policy: ReloadPolicy::Never,
}
}
}
pub struct Nickel<D: Sync + Send + 'static = ()> {
middleware_stack: MiddlewareStack<D>,
data: D,
keep_alive_timeout: Option<Duration>,
pub options: Options,
}
impl<D: Sync + Send + 'static> HttpRouter<D> for Nickel<D> {
fn add_route<M: Into<Matcher>, H: Middleware<D>>(&mut self, method: Method, matcher: M, handler: H) -> &mut Self {
let mut router = Router::new();
router.add_route(method, matcher, handler);
self.utilize(router);
self
}
}
impl Nickel<()> {
pub fn new() -> Nickel<()> {
Nickel::with_data(())
}
pub fn with_options(options: Options) -> Nickel<()> {
Nickel::with_data_and_options((), options)
}
}
impl<D: Sync + Send + 'static> Nickel<D> {
pub fn with_data_and_options(data: D, options: Options) -> Nickel<D> {
let mut middleware_stack = MiddlewareStack::new();
middleware_stack.add_error_handler(DefaultErrorHandler);
Nickel {
middleware_stack: middleware_stack,
options: options,
data: data,
keep_alive_timeout: Some(Duration::from_secs(75))
}
}
pub fn with_data(data: D) -> Nickel<D> {
Nickel::with_data_and_options(data, Options::default())
}
pub fn utilize<T: Middleware<D>>(&mut self, handler: T){
self.middleware_stack.add_middleware(handler);
}
pub fn handle_error<T: ErrorHandler<D>>(&mut self, handler: T){
self.middleware_stack.add_error_handler(handler);
}
pub fn router() -> Router<D> {
Router::new()
}
pub fn listen<T: ToSocketAddrs>(mut self, addr: T) -> Result<ListeningServer, Box<StdError>> {
self.middleware_stack.add_middleware(middleware! {
(StatusCode::NotFound, "File Not Found")
});
let server = Server::new(self.middleware_stack, self.options.reload_policy, self.data);
let is_test_harness = env::var_os("NICKEL_TEST_HARNESS").is_some();
let listener = if is_test_harness {
try!(server.serve("localhost:0",
self.keep_alive_timeout,
self.options.thread_count))
} else {
try!(server.serve(addr,
self.keep_alive_timeout,
self.options.thread_count))
};
if self.options.output_on_listen {
println!("Listening on http://{}", listener.socket());
println!("Ctrl-C to shutdown server");
}
Ok(listener)
}
pub fn keep_alive_timeout(&mut self, timeout: Option<Duration>){
self.keep_alive_timeout = timeout;
}
pub fn listen_https<T,S>(mut self, addr: T, ssl: S) -> Result<ListeningServer, Box<StdError>>
where T: ToSocketAddrs,
S: SslServer + Send + Clone + 'static {
self.middleware_stack.add_middleware(middleware! {
(StatusCode::NotFound, "File Not Found")
});
let server = Server::new(self.middleware_stack, self.options.reload_policy, self.data);
let is_test_harness = env::var_os("NICKEL_TEST_HARNESS").is_some();
let listener = if is_test_harness {
try!(server.serve_https("localhost:0",
self.keep_alive_timeout,
self.options.thread_count,
ssl))
} else {
try!(server.serve_https(addr,
self.keep_alive_timeout,
self.options.thread_count,
ssl))
};
if self.options.output_on_listen {
println!("Listening on https://{}", listener.socket());
println!("Ctrl-C to shutdown server");
}
Ok(listener)
}
}
#[cfg(test)]
mod tests {
use Nickel;
use std::str::FromStr;
use std::net::SocketAddr;
#[test]
#[should_panic(expected = "invalid socket address")]
fn invalid_listen_addr() {
Nickel::new().listen("127.0.0.1.6667").unwrap();
}
#[test]
fn can_get_server_address() {
let server = Nickel::new().listen("127.0.0.1:12345").unwrap();
assert_eq!(server.socket(), SocketAddr::from_str("127.0.0.1:12345").unwrap());
server.detach();
}
#[test]
fn can_get_server_address_with_random_port() {
let server = Nickel::new().listen("127.0.0.1:0").unwrap();
assert_eq!(server.socket().ip().to_string(), "127.0.0.1");
server.detach();
}
}