Skip to main content

HostRouter

Struct HostRouter 

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

Routes requests to different axum Routers based on the Host header.

Supports exact host matches (acme.com, app.acme.com) and single-level wildcard subdomains (*.acme.com). Both use HashMap lookups for O(1) matching. The effective host is resolved from the Forwarded (RFC 7239), X-Forwarded-Host, or Host header, in that order; the value is lowercased and any trailing :port is stripped before matching.

HostRouter implements Into<axum::Router> and can therefore be passed directly to http().

§Panics

The host and fallback methods panic if called after the HostRouter has been cloned or converted. Complete all route registration before passing the router to server::http() or cloning it.

The host method also panics on:

  • Empty host patterns
  • Duplicate exact host patterns
  • Duplicate wildcard suffixes
  • Malformed wildcard patterns (must be *.suffix where the suffix contains at least one dot; a bare * or *.com is rejected)

§Example

use modo::server::HostRouter;
use axum::Router;

let app = HostRouter::new()
    .host("acme.com", Router::new())
    .host("app.acme.com", Router::new())
    .host("*.acme.com", Router::new())
    .fallback(Router::new());

Implementations§

Source§

impl HostRouter

Source

pub fn new() -> Self

Create a new empty HostRouter with no registered hosts and no fallback.

Source

pub fn host(self, pattern: &str, router: Router) -> Self

Register a host pattern with a router.

Exact patterns (e.g. "acme.com", "app.acme.com") match the host literally. Wildcard patterns (e.g. "*.acme.com") match any single subdomain level. The pattern is trimmed, lowercased, and stripped of any :port suffix before registration.

§Panics
  • If self has already been cloned or converted to an axum::Router.
  • If the pattern is empty after trimming.
  • If a bare * or a leading * not followed by . is supplied.
  • If an exact host is registered twice.
  • If a wildcard suffix is registered twice.
  • If a wildcard suffix is empty or contains no dot (e.g. "*.com").
Source

pub fn fallback(self, router: Router) -> Self

Set a fallback router for requests whose host doesn’t match any pattern.

If no fallback is set, unmatched hosts receive a 404 response.

§Panics

Panics if self has already been cloned or converted to an axum::Router.

Trait Implementations§

Source§

impl Clone for HostRouter

Source§

fn clone(&self) -> HostRouter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HostRouter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HostRouter

Source§

fn default() -> Self

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

impl From<HostRouter> for Router

Source§

fn from(host_router: HostRouter) -> Router

Converts to this type from the input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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