pub struct Router { /* private fields */ }
Expand description
Router.
This is not to be used directly, it is only used for Direkuta.route.
All examples for routing are shown with ‘output’ or what the paths will look like and what the response would look like when called.
The format is as shown.
ⓘ
URL : { Parameter => Capture } {
Method => Response
}
Implementations§
source§impl Router
impl Router
sourcepub fn route(
&mut self,
method: Method,
path: impl Into<String>,
handler: impl Fn(Request, Arc<State>, Capture) -> Box<dyn Future<Item = Response<Body>, Error = DireError> + Send + 'static> + Send + Sync + 'static
)
pub fn route(
&mut self,
method: Method,
path: impl Into<String>,
handler: impl Fn(Request, Arc<State>, Capture) -> Box<dyn Future<Item = Response<Body>, Error = DireError> + Send + 'static> + Send + Sync + 'static
)
Adds route to routing map.
Its easier to the the helper functions.
Examples
Simple
Direkuta::new()
.route(|r| {
r.route(Method::GET, "/", |_, _, _| {
Response::new().with_body("Hello World!").build()
});
});
ⓘ
"/" {
GET => "Hello World!"
}
Regex
Direkuta::new()
.route(|r| {
r.route(Method::GET, "/<name:(.*)>", |_, _, c| {
Response::new().with_body(c.get("name")).build()
});
});
ⓘ
"/txuritan" : { "name" => "txuritan" } {
GET => "txuritan"
}
sourcepub fn get(
&mut self,
path: impl Into<String>,
handler: impl Fn(Request, Arc<State>, Capture) -> Box<dyn Future<Item = Response<Body>, Error = DireError> + Send + 'static> + Send + Sync + 'static
)
pub fn get(
&mut self,
path: impl Into<String>,
handler: impl Fn(Request, Arc<State>, Capture) -> Box<dyn Future<Item = Response<Body>, Error = DireError> + Send + 'static> + Send + Sync + 'static
)
Adds a GET request handler.
Examples
Simple
Direkuta::new()
.route(|r| {
r.get("/", |_, _, _| {
Response::new().with_body("Hello World!").build()
});
});
ⓘ
"/" : { } {
GET => "Hello World!"
}
Regex
Direkuta::new()
.route(|r| {
r.route(Method::GET, "/<name:(.*)>", |_, _, c| {
Response::new().with_body(c.get("name")).build()
});
});
ⓘ
"/txuritan" : { "name" => "txuritan" } {
GET => "txuritan"
}