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
*.suffixwhere the suffix contains at least one dot; a bare*or*.comis 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
impl HostRouter
Sourcepub fn host(self, pattern: &str, router: Router) -> Self
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
selfhas already been cloned or converted to anaxum::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").
Sourcepub fn fallback(self, router: Router) -> Self
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
impl Clone for HostRouter
Source§fn clone(&self) -> HostRouter
fn clone(&self) -> HostRouter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more