Skip to main content

RouterBuilder

Struct RouterBuilder 

Source
pub struct RouterBuilder { /* private fields */ }
Expand description

A builder for constructing the router.

Implementations§

Source§

impl RouterBuilder

Source

pub fn new() -> Self

Create an empty builder with no mounts.

Source

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.

Source

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 to Host (RFC 9112 §3.2.2). A capture matches exactly one label of letters, digits and hyphens, and is filled in lowercase. None means 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 of host. 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.js there 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().

Source

pub fn build(self) -> Router

Finalize the builder into an immutable Router.

Trait Implementations§

Source§

impl Default for RouterBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more