Struct trillium_router::Router
source · pub struct Router { /* private fields */ }
Expand description
§The Router handler
See crate level docs for more, as this is the primary type in this crate.
Implementations§
source§impl Router
impl Router
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new Router. This is often used with Router::get
,
Router::post
, Router::put
, Router::delete
, and
Router::patch
chainable methods to build up an application.
For an alternative way of constructing a Router, see Router::build
let router = Router::new()
.get("/", |conn: Conn| async move { conn.ok("you have reached the index") })
.get("/some/:param", |conn: Conn| async move { conn.ok("you have reached /some/:param") })
.post("/", |conn: Conn| async move { conn.ok("post!") });
use trillium_testing::prelude::*;
assert_ok!(get("/").on(&router), "you have reached the index");
assert_ok!(get("/some/route").on(&router), "you have reached /some/:param");
assert_ok!(post("/").on(&router), "post!");
sourcepub fn without_options_handling(self) -> Self
pub fn without_options_handling(self) -> Self
Disable the default behavior of responding to OPTIONS requests with the supported methods at a given path
sourcepub fn build(builder: impl Fn(RouterRef<'_>)) -> Router
pub fn build(builder: impl Fn(RouterRef<'_>)) -> Router
Another way to build a router, if you don’t like the chainable
interface described in Router::new
. Note that the argument to
the closure is a RouterRef
.
let router = Router::build(|mut router| {
router.get("/", |conn: Conn| async move {
conn.ok("you have reached the index")
});
router.get("/some/:paramroute", |conn: Conn| async move {
conn.ok("you have reached /some/:param")
});
router.post("/", |conn: Conn| async move {
conn.ok("post!")
});
});
use trillium_testing::prelude::*;
assert_ok!(get("/").on(&router), "you have reached the index");
assert_ok!(get("/some/route").on(&router), "you have reached /some/:param");
assert_ok!(post("/").on(&router), "post!");
sourcepub fn with_route<M, R>(self, method: M, path: R, handler: impl Handler) -> Self
pub fn with_route<M, R>(self, method: M, path: R, handler: impl Handler) -> Self
Registers a handler for a method other than get, put, post, patch, or delete.
let router = Router::new()
.with_route("OPTIONS", "/some/route", |conn: Conn| async move { conn.ok("directly handling options") })
.with_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 all<R>(self, path: R, handler: impl Handler) -> Self
pub fn all<R>(self, path: R, handler: impl Handler) -> Self
Appends the handler to all (get, post, put, delete, and patch) methods.
let router = Router::new().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>(
self,
methods: &[IntoMethod],
path: R,
handler: impl Handler,
) -> Self
pub fn any<IntoMethod, R>( self, methods: &[IntoMethod], path: R, handler: impl Handler, ) -> Self
Appends the handler to each of the provided http methods.
let router = Router::new().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 get<R>(self, path: R, handler: impl Handler) -> Self
pub fn get<R>(self, path: R, handler: impl Handler) -> Self
Registers a handler for the get http method.
let router = Router::new().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>(self, path: R, handler: impl Handler) -> Self
pub fn post<R>(self, path: R, handler: impl Handler) -> Self
Registers a handler for the post http method.
let router = Router::new().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>(self, path: R, handler: impl Handler) -> Self
pub fn put<R>(self, path: R, handler: impl Handler) -> Self
Registers a handler for the put http method.
let router = Router::new().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>(self, path: R, handler: impl Handler) -> Self
pub fn delete<R>(self, path: R, handler: impl Handler) -> Self
Registers a handler for the delete http method.
let router = Router::new().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>(self, path: R, handler: impl Handler) -> Self
pub fn patch<R>(self, path: R, handler: impl Handler) -> Self
Registers a handler for the patch http method.
let router = Router::new().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));
Trait Implementations§
source§impl Handler for Router
impl Handler for Router
source§fn run<'life0, 'async_trait>(
&'life0 self,
conn: Conn,
) -> Pin<Box<dyn Future<Output = Conn> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn run<'life0, 'async_trait>(
&'life0 self,
conn: Conn,
) -> Pin<Box<dyn Future<Output = Conn> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
source§fn before_send<'life0, 'async_trait>(
&'life0 self,
conn: Conn,
) -> Pin<Box<dyn Future<Output = Conn> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn before_send<'life0, 'async_trait>(
&'life0 self,
conn: Conn,
) -> Pin<Box<dyn Future<Output = Conn> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
source§fn has_upgrade(&self, upgrade: &Upgrade) -> bool
fn has_upgrade(&self, upgrade: &Upgrade) -> bool
Handler::upgrade
. The first
handler that responds true to this will receive ownership of the
trillium::Upgrade
in a subsequent call to Handler::upgrade
source§fn upgrade<'life0, 'async_trait>(
&'life0 self,
upgrade: Upgrade,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn upgrade<'life0, 'async_trait>(
&'life0 self,
upgrade: Upgrade,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handler::has_upgrade
and will only be called once for this
upgrade. There is no return value, and this function takes
exclusive ownership of the underlying transport once this is
called. You can downcast the transport to whatever the source
transport type is and perform any non-http protocol communication
that has been negotiated. You probably don’t want this unless
you’re implementing something like websockets. Please note that
for many transports such as TcpStreams, dropping the transport
(and therefore the Upgrade) will hang up / disconnect.