Crate trillium_router[][src]

Expand description

Welcome to the trillium router crate!

This router is built on top of routefinder, and the details of route resolution and definition are documented on that repository.

use trillium::{conn_unwrap, Conn};
use trillium_router::{Router, RouterConnExt};

let router = Router::new()
    .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, conn.param("page_name"));
        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));

Although this is currently the only trillium router, it is an important aspect of trillium’s architecture that the router uses only public apis and is interoperable with other router implementations. If you have different ideas of how a router might work, please publish a crate! It should be possible to nest different types of routers (and different versions of router crates) within each other as long as they all depend on the same version of the trillium crate.

Macros

routes

The routes macro represents an experimental macro for defining routers.

Structs

Router

The Router handler

RouterRef

A &mut Router for use with Router::build

Traits

RouterConnExt

Extends trillium::Conn with accessors for router params.