Struct iron_mountrouter::Router [] [src]

pub struct Router {
    // some fields omitted
}

Router provides an interface to apply different handlers to different routes as middleware for the Iron framework.

Methods

impl Router

fn new() -> Router

Construct new Router.

let router = Router::new();

fn add_route<H, S>(&mut self, route: S, handler: H, is_mounted: bool) -> &mut Router where H: Handler, S: AsRef<str>

Add a new route to a Router, matching given pattern.

route is a pattern supported by route recognizer:

string like /static/page/here is matched entirely;

:name is a dynamic param, matching any string that does not contain slash /, i.e.

/page/:page-num/content will match /page/17/content or /page/abbra-t/content but not /page/17/5/content.

Colon only works at the start of the segment so /page-:num works like usual string match and will not match /page-5.

Currently there is no way to set param type so it catches only strings and does not perform any additional checks.

*name is a dynamic wildcard param, matching any string (even those containing slashes/). /redirect/to/*path will match /redirect/to/example.com/example.

Captured parameters are stored in Params type and can be accessed in request extensions:

let ref params = req.extensions.get::<Router>().unwrap()

Unwrap is safe here because router always adds extension if route is matched, even if no params were captured.

handler is any Iron handler. Working with mountrouter you will most often use MethodPicker to pick appropriate handler for given http method.

If is_mounted is true, router will match any string starting with route. Original url is preserved, while stripped part is stored in request extensions using StrippedUrl. Further Routers will used stripped url to match their routes. For example,

let mut router = Router::new();
router.add_route("/book/:page/", handler, true);
fn handler(req: &mut Request) -> IronResult<Response> {
    // for path "/book/17/page/3"

    // ["book", "17", "page", "3"]
    println!("{:?}", req.url.path);
    // ["page", "3"]
    println!("{:?}", req.extensions.get::<StrippedUrl>().unwrap().path);
}

If is_mounted is set, route is forced to end with slash: slash is appended to the end of route if it is not there.

Trait Implementations

impl Key for Router

type Value = Params

impl Handler for Router

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