Skip to content

Commit

Permalink
Auto merge of #270 - stephenmuss:route_chaining, r=Ryman
Browse files Browse the repository at this point in the history
feat(router): add chainable routes

Closes #262
  • Loading branch information
homu committed Aug 25, 2015
2 parents 4efdb8b + 2dd98e8 commit c5dff10
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ path = "examples/moved_ownership.rs"

name = "example_route_data"
path = "examples/example_route_data.rs"

[[example]]
name = "chaining"
path = "examples/chaining.rs"
18 changes: 18 additions & 0 deletions examples/chaining.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[macro_use] extern crate nickel;
extern crate hyper;

use hyper::method::Method;
use nickel::{Nickel, HttpRouter};

fn main() {
let mut server = Nickel::new();
server
.add_route(Method::Get, "/", middleware!("Hello World"))
.get("/get", middleware!("get"))
.post("/post", middleware!("post"))
.put("/put", middleware!("put"))
.patch("/patch", middleware!("patch"))
.delete("/delete", middleware!("delete"));

server.listen("127.0.0.1:6767");
}
3 changes: 2 additions & 1 deletion src/nickel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ pub struct Nickel{
}

impl HttpRouter for Nickel {
fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, method: Method, matcher: M, handler: H) {
fn add_route<M: Into<Matcher>, H: Middleware>(&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
}
}

Expand Down
41 changes: 28 additions & 13 deletions src/router/http_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait HttpRouter {
/// server.add_route(Get, regex, middleware! { "Regex Get request! "});
/// }
/// ```
fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, Method, M, H);
fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, Method, M, H) -> &mut Self;

/// Registers a handler to be used for a specific GET request.
/// Handlers are assigned to paths and paths are allowed to contain
Expand Down Expand Up @@ -71,6 +71,21 @@ pub trait HttpRouter {
/// server.get("/user/**/:userid", middleware! {
/// "This matches /user/list/4711 and also /user/extended/list/4711"
/// });
///
/// // with chained routes
/// server
/// .get("/foo", middleware! {
/// "foo"
/// })
/// .get("/bar", middleware! {
/// "bar"
/// })
/// .get("/baz", middleware! {
/// "baz"
/// })
/// .get("/quux", middleware! {
/// "quux"
/// });
/// }
/// ```
///
Expand Down Expand Up @@ -106,42 +121,42 @@ pub trait HttpRouter {
/// server.utilize(router);
/// }
/// ```
fn get<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Get, matcher, handler);
fn get<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Get, matcher, handler)
}

/// Registers a handler to be used for a specific POST request.
///
/// Take a look at `get(...)` for a more detailed description.
fn post<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Post, matcher, handler);
fn post<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Post, matcher, handler)
}

/// Registers a handler to be used for a specific PUT request.
///
/// Take a look at `get(...)` for a more detailed description.
fn put<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Put, matcher, handler);
fn put<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Put, matcher, handler)
}

/// Registers a handler to be used for a specific DELETE request.
///
/// Take a look at `get(...)` for a more detailed description.
fn delete<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Delete, matcher, handler);
fn delete<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Delete, matcher, handler)
}

/// Registers a handler to be used for a specific OPTIONS request.
///
/// Take a look at `get(...)` for a more detailed description.
fn options<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Options, matcher, handler);
fn options<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Options, matcher, handler)
}

/// Registers a handler to be used for a specific PATCH request.
///
/// Take a look at `get(...)` for a more detailed description.
fn patch<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) {
self.add_route(Method::Patch, matcher, handler);
fn patch<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H) -> &mut Self {
self.add_route(Method::Patch, matcher, handler)
}
}
3 changes: 2 additions & 1 deletion src/router/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ fn extract_params(route: &Route, path: &str) -> Vec<(String, String)> {
}

impl HttpRouter for Router {
fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, method: Method, matcher: M, handler: H) {
fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, method: Method, matcher: M, handler: H) -> &mut Self {
let route = Route {
matcher: matcher.into(),
method: method,
handler: Box::new(handler),
};

self.routes.push(route);
self
}
}

Expand Down

0 comments on commit c5dff10

Please sign in to comment.