Struct Router

Source
pub struct Router { /* private fields */ }
Expand description

Router provides an interface for creating complex routes as middleware for the Iron framework.

Implementations§

Source§

impl Router

Source

pub fn new() -> Router

Construct a new, empty Router.

let router = Router::new();
Examples found in repository?
examples/custom_404.rs (line 28)
27fn main() {
28    let mut router = Router::new();
29    router.get("/", handler, "example");
30
31    let mut chain = Chain::new(router);
32    chain.link_after(Custom404);
33
34    Iron::new(chain).http("localhost:3000").unwrap();
35}
More examples
Hide additional examples
examples/struct_handler.rs (line 27)
22fn main() {
23    let handler = MessageHandler {
24        message: "You've found the index page!".to_string()
25    };
26
27    let mut router = Router::new();
28    router.get("/", handler, "index");
29
30    Iron::new(router).http("localhost:3000").unwrap();
31}
examples/simple.rs (line 13)
12fn main() {
13    let mut router = Router::new();
14    router.get("/", handler, "handler");
15    router.get("/:query", query_handler, "query_handler");
16
17    Iron::new(router).http("localhost:3000").unwrap();
18
19    fn handler(_: &mut Request) -> IronResult<Response> {
20        Ok(Response::with((status::Ok, "OK")))
21    }
22
23    fn query_handler(req: &mut Request) -> IronResult<Response> {
24        let ref query = req.extensions.get::<Router>()
25            .unwrap().find("query").unwrap_or("/");
26        Ok(Response::with((status::Ok, *query)))
27    }
28
29
30}
Source

pub fn route<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, method: Method, glob: S, handler: H, route_id: I, ) -> &mut Router

Add a new route to a Router, matching both a method and glob pattern.

route supports glob patterns: * for a single wildcard segment and :param for matching storing that segment of the request url in the Params object, which is stored in the request extensions.

For instance, to route Get requests on any route matching /users/:userid/:friend and store userid and friend in the exposed Params object:

let mut router = Router::new();
router.route(method::Get, "/users/:userid/:friendid", controller, "user_friend");

route_id is a unique name for your route, and is used when generating an URL with url_for.

The controller provided to route can be any Handler, which allows extreme flexibility when handling routes. For instance, you could provide a Chain, a Handler, which contains an authorization middleware and a controller function, so that you can confirm that the request is authorized for this route before handling it.

Source

pub fn get<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Get method.

Examples found in repository?
examples/custom_404.rs (line 29)
27fn main() {
28    let mut router = Router::new();
29    router.get("/", handler, "example");
30
31    let mut chain = Chain::new(router);
32    chain.link_after(Custom404);
33
34    Iron::new(chain).http("localhost:3000").unwrap();
35}
More examples
Hide additional examples
examples/struct_handler.rs (line 28)
22fn main() {
23    let handler = MessageHandler {
24        message: "You've found the index page!".to_string()
25    };
26
27    let mut router = Router::new();
28    router.get("/", handler, "index");
29
30    Iron::new(router).http("localhost:3000").unwrap();
31}
examples/simple.rs (line 14)
12fn main() {
13    let mut router = Router::new();
14    router.get("/", handler, "handler");
15    router.get("/:query", query_handler, "query_handler");
16
17    Iron::new(router).http("localhost:3000").unwrap();
18
19    fn handler(_: &mut Request) -> IronResult<Response> {
20        Ok(Response::with((status::Ok, "OK")))
21    }
22
23    fn query_handler(req: &mut Request) -> IronResult<Response> {
24        let ref query = req.extensions.get::<Router>()
25            .unwrap().find("query").unwrap_or("/");
26        Ok(Response::with((status::Ok, *query)))
27    }
28
29
30}
Source

pub fn post<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Post method.

Source

pub fn put<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Put method.

Source

pub fn delete<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Delete method.

Source

pub fn head<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Head method.

Source

pub fn patch<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Patch method.

Source

pub fn options<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Like route, but specialized to the Options method.

Source

pub fn any<S: AsRef<str>, H: Handler, I: AsRef<str>>( &mut self, glob: S, handler: H, route_id: I, ) -> &mut Router

Route will match any method, including gibberish. In case of ambiguity, handlers specific to methods will be preferred.

Trait Implementations§

Source§

impl Handler for Router

Source§

fn handle(&self, req: &mut Request<'_, '_>) -> IronResult<Response>

Produce a Response from a Request, with the possibility of error.
Source§

impl Key for Router

Source§

type Value = Params

The value type associated with this key type.

Auto Trait Implementations§

§

impl Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Typeable for T
where T: Any,

Source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
Source§

impl<T> UnsafeAny for T
where T: Any,