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
impl Router
Sourcepub fn new() -> Router
pub fn new() -> Router
Construct a new, empty Router
.
let router = Router::new();
Examples found in repository?
More examples
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}
Sourcepub fn route<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
method: Method,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.
Sourcepub fn get<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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?
More examples
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}
Sourcepub fn post<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.
Sourcepub fn put<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.
Sourcepub fn delete<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.
Sourcepub fn head<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.
Sourcepub fn patch<S: AsRef<str>, H: Handler, I: AsRef<str>>(
&mut self,
glob: S,
handler: H,
route_id: I,
) -> &mut Router
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.