pub struct RouterBuilder { /* private fields */ }Expand description
A builder for constructing the router.
Implementations§
Source§impl RouterBuilder
impl RouterBuilder
Sourcepub fn add_route(self, path: &str, controller: Arc<dyn Controller>) -> Self
pub fn add_route(self, path: &str, controller: Arc<dyn Controller>) -> Self
Adds a route, creating the necessary nodes in the tree.
A trailing * segment is optional sugar for mounting the
controller at the prefix before it: "foo/*" routes identically to
"foo", and "*" identically to "" (the root) — a mounted
controller already receives the entire unconsumed path as its action,
so a “catch-all” is just a mount. The * is there so a reader of
app_routes! can see “this controller is the catch-all here” at a
glance. A * anywhere but the last segment is meaningless; such a
route is dropped with a warning.
Sourcepub fn host_alias(
self,
host: Option<&str>,
target: &str,
shares: &[&str],
) -> Result<Self, HostAliasError>
pub fn host_alias( self, host: Option<&str>, target: &str, shares: &[&str], ) -> Result<Self, HostAliasError>
Make a host a name for a mounted controller — a host alias.
A request whose host host names is routed as the request for
target followed by the request’s own path: on acme.example.com,
with host "{tenant}.example.com" and target
"tenants/{tenant}", GET /orders/7 is routed as
GET /tenants/acme/orders/7. The controller mounted at tenants
receives the action acme/orders/7, and its own routes, typed
parameters, verb rules, body cap, rate-limit class and floor apply as
for any request. Once it reaches the controller, it is regular routing.
host— dot-separated labels, each a literal hostname label or a{name}capture. Matching ignores ASCII case, the port and a trailing dot, and reads an absolute-form request’s authority in preference toHost(RFC 9112 §3.2.2). A capture matches exactly one label of letters, digits and hyphens, and is filled in lowercase.Nonemeans the alias does not exist in this deployment: the entry is still checked, but nothing is registered.target— a mounted controller’s path, then one{name}segment per capture ofhost. It must name a mount exactly, so every aliased path reaches that controller.shares— other mounts that keep their own paths on the aliased host (/assets/app.jsthere is the app’s/assets/app.js). Each must be exactly a mount, and none may contain the aliased one.
On an aliased host nothing else is reachable: every path outside the
shared mounts goes under target. Aliases are tried in the order they
were added and the first that names the host wins; a host that none
names is routed exactly as without aliases.
One address. The server applies the alias before anything else
sees the request, so Request::path_parts
holds the aliased path (tenants/acme/orders/7) for the router, for
every middleware and for the handler alike, and Host still names the
host. A handler learns that an alias routed its request, and under
which prefix, from Params::alias_prefix. An aliased request is
identical, apart from Host, to a request any client could send to the
aliased path directly: an alias adds addresses, never capabilities.
Add the routes first: target and shares are checked against the
mounts present when this is called. The hosts block of app_routes!
generates these calls after its routes and returns the error from
init().