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 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")
});
let app = TestServer::new(router).await;
app.get("/some/route").await
.assert_ok()
.assert_body("success");
app.get("/other/route").await
.assert_status(404);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")
});
let app = TestServer::new(router).await;
app.post("/some/route").await
.assert_ok()
.assert_body("success");
app.post("/other/route").await
.assert_status(404);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")
});
let app = TestServer::new(router).await;
app.put("/some/route").await
.assert_ok()
.assert_body("success");
app.put("/other/route").await
.assert_status(404);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")
});
let app = TestServer::new(router).await;
app.delete("/some/route").await
.assert_ok()
.assert_body("success");
app.delete("/other/route").await
.assert_status(404);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")
});
let app = TestServer::new(router).await;
app.patch("/some/route").await
.assert_ok()
.assert_body("success");
app.patch("/other/route").await
.assert_status(404);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!") });
let app = TestServer::new(router).await;
app.get("/")
.await
.assert_ok()
.assert_body("you have reached the index");
app.get("/some/route")
.await
.assert_ok()
.assert_body("you have reached /some/:param");
app.post("/").await.assert_ok().assert_body("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!") });
});
let app = TestServer::new(router).await;
app.get("/")
.await
.assert_ok()
.assert_body("you have reached the index");
app.get("/some/route")
.await
.assert_ok()
.assert_body("you have reached /some/:param");
app.post("/").await.assert_ok().assert_body("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??")
});
let app = TestServer::new(router).await;
app.build(Method::Options, "/some/route")
.await
.assert_ok()
.assert_body("directly handling options");
app.build(Method::Checkin, "/some/route")
.await
.assert_ok()
.assert_body("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)
});
let app = TestServer::new(router).await;
app.get("/any")
.await
.assert_ok()
.assert_body("you made a GET request to /any");
app.post("/any")
.await
.assert_ok()
.assert_body("you made a POST request to /any");
app.delete("/any")
.await
.assert_ok()
.assert_body("you made a DELETE request to /any");
app.patch("/any")
.await
.assert_ok()
.assert_body("you made a PATCH request to /any");
app.put("/any")
.await
.assert_ok()
.assert_body("you made a PUT request to /any");
app.get("/").await.assert_status(404);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)
});
let app = TestServer::new(router).await;
app.get("/get_or_post")
.await
.assert_ok()
.assert_body("you made a GET request to /get_or_post");
app.post("/get_or_post")
.await
.assert_ok()
.assert_body("you made a POST request to /get_or_post");
app.delete("/any").await.assert_status(404);
app.patch("/any").await.assert_status(404);
app.put("/any").await.assert_status(404);
app.get("/").await.assert_status(404);Trait Implementations§
Source§impl Handler for Router
impl Handler for Router
Source§async fn run(&self, conn: Conn) -> Conn
async fn run(&self, conn: Conn) -> Conn
Source§async fn before_send(&self, conn: Conn) -> Conn
async fn before_send(&self, conn: Conn) -> Conn
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::upgradeSource§async fn upgrade(&self, upgrade: Upgrade)
async fn upgrade(&self, upgrade: Upgrade)
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.