Struct peta::Router

source ·
pub struct Router { /* private fields */ }
Expand description

Generates map between path and method which returns ResponseFut supports * and : operators.

Example

let mut router = Router::new();

router.get("/", |req: Request| {
  // do not forget to return ResponseFut
});

router.post("/home", |req: Request| {
  // do not forget to return ResponseFut
});

// It is important to add default route
// can be simple 404 which will be called if nothing found
router.add_default(|req: Request| {});

let router = router.build();

Implementations§

Create instance of Router.

let router = Router::new();

Wrap router in Arc to be able to pass it across threads/components.

let mut router = Router::new();
// do some things with router variable

let router = router.build();

// now we can simply clone and call any functions from router instance
let ref_router = router.clone();

Adds new path -> method map to the Router.

router.add(method::GET, "/", |req: Request| {});
router.add(method::POST, "/", |req: Request| {});
// and so on

Searches for appropriate method which is mapped to specific path

let req: request::Request;
// it will automatically extract path from `req`
router.find(req)

Set default function for routes which were not mapped can be simple 404 response.

router.add_default(|req: Request| {});

Abstracts add method by removing method::* param

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.