Struct trillium_router::RouterRef
source · pub struct RouterRef<'r>(/* private fields */);
Expand description
A &mut Router
for use with Router::build
A wrapper around a &mut Router
that supports imperative route
registration. See Router::build
for further documentation.
Implementations§
source§impl<'r> RouterRef<'r>
impl<'r> RouterRef<'r>
sourcepub fn get<R>(&mut self, path: R, handler: impl Handler)
pub fn get<R>(&mut self, path: R, handler: impl Handler)
Registers a handler for the get http method.
let router = Router::build(|mut router| {
router.get("/some/route", |conn: Conn| async move {
conn.ok("success")
});
});
use trillium_testing::{methods::get, assert_ok, assert_not_handled};
assert_ok!(get("/some/route").on(&router), "success");
assert_not_handled!(get("/other/route").on(&router));
sourcepub fn post<R>(&mut self, path: R, handler: impl Handler)
pub fn post<R>(&mut self, path: R, handler: impl Handler)
Registers a handler for the post http method.
let router = Router::build(|mut router| {
router.post("/some/route", |conn: Conn| async move {
conn.ok("success")
});
});
use trillium_testing::{methods::post, assert_ok, assert_not_handled};
assert_ok!(post("/some/route").on(&router), "success");
assert_not_handled!(post("/other/route").on(&router));
sourcepub fn put<R>(&mut self, path: R, handler: impl Handler)
pub fn put<R>(&mut self, path: R, handler: impl Handler)
Registers a handler for the put http method.
let router = Router::build(|mut router| {
router.put("/some/route", |conn: Conn| async move {
conn.ok("success")
});
});
use trillium_testing::{methods::put, assert_ok, assert_not_handled};
assert_ok!(put("/some/route").on(&router), "success");
assert_not_handled!(put("/other/route").on(&router));
sourcepub fn delete<R>(&mut self, path: R, handler: impl Handler)
pub fn delete<R>(&mut self, path: R, handler: impl Handler)
Registers a handler for the delete http method.
let router = Router::build(|mut router| {
router.delete("/some/route", |conn: Conn| async move {
conn.ok("success")
});
});
use trillium_testing::{methods::delete, assert_ok, assert_not_handled};
assert_ok!(delete("/some/route").on(&router), "success");
assert_not_handled!(delete("/other/route").on(&router));
sourcepub fn patch<R>(&mut self, path: R, handler: impl Handler)
pub fn patch<R>(&mut self, path: R, handler: impl Handler)
Registers a handler for the patch http method.
let router = Router::build(|mut router| {
router.patch("/some/route", |conn: Conn| async move {
conn.ok("success")
});
});
use trillium_testing::{methods::patch, assert_ok, assert_not_handled};
assert_ok!(patch("/some/route").on(&router), "success");
assert_not_handled!(patch("/other/route").on(&router));
sourcepub fn all<R>(&mut self, path: R, handler: impl Handler)
pub fn all<R>(&mut self, path: R, handler: impl Handler)
Appends the handler to all (get, post, put, delete, and patch) methods.
let router = Router::build(|mut router| {
router.all("/any", |conn: Conn| async move {
let response = format!("you made a {} request to /any", conn.method());
conn.ok(response)
});
});
use trillium_testing::prelude::*;
assert_ok!(get("/any").on(&router), "you made a GET request to /any");
assert_ok!(post("/any").on(&router), "you made a POST request to /any");
assert_ok!(delete("/any").on(&router), "you made a DELETE request to /any");
assert_ok!(patch("/any").on(&router), "you made a PATCH request to /any");
assert_ok!(put("/any").on(&router), "you made a PUT request to /any");
assert_not_handled!(get("/").on(&router));
sourcepub fn any<IntoMethod, R>(
&mut self,
methods: &[IntoMethod],
path: R,
handler: impl Handler
)
pub fn any<IntoMethod, R>( &mut self, methods: &[IntoMethod], path: R, handler: impl Handler )
Appends the handler to each of the provided http methods.
let router = Router::build(|mut router|{
router.any(&["get", "post"], "/get_or_post", |conn: Conn| async move {
let response = format!("you made a {} request to /get_or_post", conn.method());
conn.ok(response)
});
});
use trillium_testing::prelude::*;
assert_ok!(get("/get_or_post").on(&router), "you made a GET request to /get_or_post");
assert_ok!(post("/get_or_post").on(&router), "you made a POST request to /get_or_post");
assert_not_handled!(delete("/any").on(&router));
assert_not_handled!(patch("/any").on(&router));
assert_not_handled!(put("/any").on(&router));
assert_not_handled!(get("/").on(&router));
sourcepub fn add_route<M, R>(&mut self, method: M, path: R, handler: impl Handler)
pub fn add_route<M, R>(&mut self, method: M, path: R, handler: impl Handler)
Registers a handler for a method other than get, put, post, patch, or delete.
let router = Router::build(|mut router| {
router.add_route("OPTIONS", "/some/route", |conn: Conn| async move {
conn.ok("directly handling options")
});
router.add_route(Method::Checkin, "/some/route", |conn: Conn| async move {
conn.ok("checkin??")
});
});
use trillium_testing::{prelude::*, TestConn};
assert_ok!(TestConn::build(Method::Options, "/some/route", ()).on(&router), "directly handling options");
assert_ok!(TestConn::build("checkin", "/some/route", ()).on(&router), "checkin??");
sourcepub fn set_options_handling(&mut self, options_enabled: bool)
pub fn set_options_handling(&mut self, options_enabled: bool)
enable or disable the router’s behavior of responding to OPTIONS requests with the supported methods at given path.
default: enabled
see crate-level docs for further explanation of the default behavior.