Macro trillium_router::routes[][src]

macro_rules! routes {
    ($($method : ident $path : literal $(->) ? $handler : expr), + $(,) ?) => { ... };
}
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};

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)
    }
);

use trillium_testing::prelude::*;
assert_ok!(get("/").on(&router), "you have reached the index");
assert_ok!(get("/pages/trillium").on(&router), "you have reached the page named trillium");
assert_not_handled!(get("/unknown/route").on(&router));