Struct Router

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

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

Implementations§

Source§

impl Router

Source

pub fn new() -> Router

Construct a new, empty Router.

use ferrum_router::Router;
let router = Router::new();
Examples found in repository?
examples/simple.rs (line 22)
21fn main() {
22    let mut router = Router::new();
23    router.get("/", handler, None);
24    router.get("/{query}", query_handler, None);
25
26    Ferrum::new(router).http("localhost:3000").unwrap();
27}
More examples
Hide additional examples
examples/custom_404.rs (line 28)
27fn main() {
28    let mut router = Router::new();
29    router.get("/", handler, None);
30
31    let mut chain = Chain::new(router);
32    chain.link_after(Custom404);
33
34    Ferrum::new(chain).http("localhost:3000").unwrap();
35}
examples/struct_handler.rs (line 22)
17fn main() {
18    let handler = MessageHandler {
19        message: "You've found the index page!".to_string()
20    };
21
22    let mut router = Router::new();
23    router.get("/", handler, None);
24
25    Ferrum::new(router).http("localhost:3000").unwrap();
26}
examples/simple_with_types.rs (line 42)
38fn main() {
39    let mut types = DefaultStore::with_default_types();
40    types.insert("id_type", "[1-9][0-9]*");
41
42    let mut router = Router::new();
43    router.get("/", handler, None);
44    router.get(("/{id:id_type}", &types), id_handler, None);
45    router.get(("/{query:string}", &types), query_handler, None);
46
47    Ferrum::new(router).http("localhost:3000").unwrap();
48}
Source

pub fn route<G, H, S, T>( &mut self, method: Method, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

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

route supports glob patterns based on the rust regex and uses {name} ({name: typename}, {name: pattern}) for matching storing named 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:[0-9]+}/{friendid:[0-9]+} and store userid and friend in the exposed Params object:

let mut router = Router::new();
router.route(Method::Get, "/users/{userid:[0-9]+}/{friendid:[0-9]+}", controller, "user_friend");

route_id is a optional unique name for your route, and is used when generating an URI 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<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Get method.

Examples found in repository?
examples/simple.rs (line 23)
21fn main() {
22    let mut router = Router::new();
23    router.get("/", handler, None);
24    router.get("/{query}", query_handler, None);
25
26    Ferrum::new(router).http("localhost:3000").unwrap();
27}
More examples
Hide additional examples
examples/custom_404.rs (line 29)
27fn main() {
28    let mut router = Router::new();
29    router.get("/", handler, None);
30
31    let mut chain = Chain::new(router);
32    chain.link_after(Custom404);
33
34    Ferrum::new(chain).http("localhost:3000").unwrap();
35}
examples/struct_handler.rs (line 23)
17fn main() {
18    let handler = MessageHandler {
19        message: "You've found the index page!".to_string()
20    };
21
22    let mut router = Router::new();
23    router.get("/", handler, None);
24
25    Ferrum::new(router).http("localhost:3000").unwrap();
26}
examples/simple_with_types.rs (line 43)
38fn main() {
39    let mut types = DefaultStore::with_default_types();
40    types.insert("id_type", "[1-9][0-9]*");
41
42    let mut router = Router::new();
43    router.get("/", handler, None);
44    router.get(("/{id:id_type}", &types), id_handler, None);
45    router.get(("/{query:string}", &types), query_handler, None);
46
47    Ferrum::new(router).http("localhost:3000").unwrap();
48}
Source

pub fn post<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Post method.

Source

pub fn put<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Put method.

Source

pub fn delete<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Delete method.

Source

pub fn head<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Head method.

Source

pub fn patch<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Patch method.

Source

pub fn options<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

Like route, but specialized to the Options method.

Source

pub fn any<G, H, S, T>( &mut self, glob: G, handler: H, route_id: Option<Id>, ) -> &mut Router
where G: Into<Glob<S, T>>, H: Handler, S: AsRef<[u8]>, T: GlobTypes,

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, request: &mut Request) -> FerrumResult<Response>

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

impl Key for Router

Source§

type Value = BTreeMap<String, String>

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> UnsafeAny for T
where T: Any,