Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): Add chainable routes #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@ path = "examples/chaining.rs"

[[example]]

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

[[example]]

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

use nickel::{Nickel};
use nickel::router::chainable_router::{ChainablePath, ChainableHandler};

fn main() {
let mut server = Nickel::new();
server
.route("/")
.get(middleware!("get"))
.post(middleware!("post"))
.put(middleware!("put"))
.patch(middleware!("patch"))
.delete(middleware!("delete"))
.route("/chain1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about chaining route here, it feels a bit at odds with rust's ownership model here, where there's a non-obvious change to the receiver in the middle of the chain.

I find it a lot less clear than splitting it out into:

server.route("/")
      .get(...)
      .post(..);

server.route("/chain1")
      .get(..)
      .post(..);

Perhaps it's worth not having the feature at first and then seeing if the ask appears in future?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree chaining route here is a little weird. I'll remove that. That was kind of an afterthought and I didn't really think it through very clearly.

.get(middleware!("chain1"))
.route("/chain2")
.get(middleware!("chain2"));

server.listen("127.0.0.1:6767");
}
71 changes: 71 additions & 0 deletions src/router/chainable_router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use hyper::method::Method;
use middleware::Middleware;
use nickel::Nickel;
use router::http_router::{HttpRouter};

pub struct ChainableRoute<'a, D: Sync + Send + 'static = ()> {
path: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be &'a str, or you might need to introduce a new lifetime 'b if the route chaining is to work (haven't tried)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm trying to hack around my limitations in understanding lifetimes. I'll try that and see if I can get it working with an &str.

nickel: &'a mut Nickel<D>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this should be more generic so it can handle:

let mut x = Router::new();
x.route(..)
 .get(..)
 .post(..);

I think if people want to structure their app they probably want to deal with subrouters (which they can mount at certain endpoints) rather than passing around Nickel instances.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps &mut R where R: HttpRouter

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that subrouters are desirable. I read in one of the other issues that someone was looking to nest routes. I need to sit down with this for a bit and give it some thought though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could definitely be a future feature! Feel free to ask if you need anything

}

pub trait ChainablePath<'a, D> {
fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D>;
}

impl<'a, D: Sync + Send + 'static> ChainablePath<'a, D> for Nickel<D> {
fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D> {
ChainableRoute {
path: path.to_string(),
nickel: self
}
}
}

pub trait ChainableHandler<'a, D> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these methods could just be inherent methods, as it seems a bit much to require two trait imports for one feature?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thanks I'll give that a try.

fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D>;
fn get<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
fn post<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
fn delete<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
fn options<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
fn patch<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
fn put<H: Middleware<D>>(&mut self, handler: H) -> &mut Self;
}

impl<'a, D: Sync + Send + 'static> ChainableHandler<'a, D> for ChainableRoute<'a, D> {
fn route(&'a mut self, path: &str) -> ChainableRoute<'a, D> {
ChainableRoute {
path: path.to_string(),
nickel: self.nickel
}
}
fn get<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Get, &path[..], handler);
self
}
fn post<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Post, &path[..], handler);
self
}
fn delete<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Delete, &path[..], handler);
self
}
fn options<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Options, &path[..], handler);
self
}
fn patch<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Patch, &path[..], handler);
self
}
fn put<H: Middleware<D>>(&mut self, handler: H) -> &mut Self {
let path = self.path.clone();
self.nickel.add_route(Method::Put, &path[..], handler);
self
}
}
1 change: 1 addition & 0 deletions src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub use self::into_matcher::FORMAT_PARAM;

pub mod http_router;
pub mod router;
pub mod chainable_router;
mod matcher;
mod into_matcher;