-
Notifications
You must be signed in to change notification settings - Fork 157
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
.get(middleware!("chain1")) | ||
.route("/chain2") | ||
.get(middleware!("chain2")); | ||
|
||
server.listen("127.0.0.1:6767"); | ||
} |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
There was a problem hiding this comment.
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:
Perhaps it's worth not having the feature at first and then seeing if the ask appears in future?
There was a problem hiding this comment.
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.