Skip to main content

routes

Macro routes 

Source
macro_rules! routes {
    ($($method:ident $path:literal $(-> )?$handler:expr_2021),+ $(,)?) => { ... };
}
Expand description

The routes macro represents an experimental macro for defining routers.

stability note: this may be removed entirely if it is not widely used. please open an issue if you like it, or if you have ideas to improve it.

use trillium::{conn_unwrap, Conn};
use trillium_router::{routes, RouterConnExt};
use trillium_testing::TestServer;

let router = routes!(
get "/" |conn: Conn| async move { conn.ok("you have reached the index") },
get "/pages/:page_name" |conn: Conn| async move {
let page_name = conn_unwrap!(conn.param("page_name"), conn);
let content = format!("you have reached the page named {}", page_name);
conn.ok(content)
}
);

let app = TestServer::new(router).await;
app.get("/").await
    .assert_ok()
    .assert_body("you have reached the index");
app.get("/pages/trillium").await
    .assert_ok()
    .assert_body("you have reached the page named trillium");
app.get("/unknown/route").await
    .assert_status(404);