-
Notifications
You must be signed in to change notification settings - Fork 157
/
routing.rs
55 lines (43 loc) · 1.85 KB
/
routing.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
#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter};
use hyper::Method;
#[tokio::main]
async fn main() {
let mut server = Nickel::new();
// Nickel provides a default router on the server for getting
// up and running quickly. If you want to partition out your app
// you might want to use an explicit router though.
server.utilize(explicit_router());
// Most common HTTP verbs are extension methods added from the HttpRouter trait.
// You can see other examples such as 'json' to see other verbs in use.
// For other HTTP verbs, you can use the `add_route` method.
// go to http://localhost:6767/bar to see this route in action
server.add_route(Method::GET, "/bar", middleware! {
"This is the /bar handler"
});
// go to http://localhost:6767/foo to see this route in action
server.get("/:foo", middleware! { |request|
let foo = request.param("foo").unwrap();
let format = request.param("format").unwrap();
format!("Foo is '{}'. The requested format is '{}'", foo, format)
});
server.listen("127.0.0.1:6767").await.unwrap();
}
fn explicit_router() -> nickel::Router {
let mut router = Nickel::router();
// Wildcard '*' routes are supported
// - '*' will match a single directories seperated by '/'
// - '**' will match potentially many directories
// Single wildcard:
// go to http://localhost:6767/some/crazy/route to see this route in action
router.get("/some/*/route", middleware! {
"This matches /some/crazy/route but not /some/super/crazy/route"
});
// Double wildcards:
// go to http://localhost:6767/a/nice/route
// or http://localhost:6767/a/super/nice/route to see this route in action
router.get("/a/**/route", middleware! {
"This matches /a/crazy/route and also /a/super/crazy/route"
});
router
}