pub struct Stack { /* private fields */ }Expand description
The stack around a product, with the routes it serves beside its base path and the fallback of a request no route matched.
Stack::into_router mounts the product and the health routes under
the base path, as app does, the routes of Stack::beside at the
root next to them, and the handler of Stack::fallback for every
request no route matched. Every layer of app covers all three, and
the overload bound covers the product’s routes and the routes beside.
§Examples
use axum::Router;
use axum::routing::get;
use ferrohealth_health::lifecycle::Lifecycle;
use ferrohealth_health::registry::Registry;
use ferrohealth_server::health::Health;
use ferrohealth_server::section::{BasePath, ServerSection};
use ferrohealth_server::stack::{Stack, StackPolicy};
use http::StatusCode;
let section = ServerSection {
base_path: BasePath::new("/ehr")?,
..ServerSection::default()
};
let health = Health::new(Registry::default(), Lifecycle::default());
// `/ehr/v1/ehr` and `/ehr/health` under the base path, `/fhir/metadata`
// beside it, and the product's own `404` for every other path.
let product = Router::new().route("/v1/ehr", get(|| async { "ehr" }));
let fhir = Router::new().route("/fhir/metadata", get(|| async { "metadata" }));
let app: Router = Stack::new(product, health, §ion, StackPolicy::default())
.beside(fhir)
.fallback(|| async { (StatusCode::NOT_FOUND, "no such resource") })
.into_router();Implementations§
Source§impl Stack
impl Stack
Sourcepub fn new(
product: Router,
health: Health,
section: &ServerSection,
policy: StackPolicy,
) -> Self
pub fn new( product: Router, health: Health, section: &ServerSection, policy: StackPolicy, ) -> Self
The stack around product and health under section, with
policy: what app builds.
Sourcepub fn beside(self, routes: Router) -> Self
pub fn beside(self, routes: Router) -> Self
Mounts routes at the root, beside the base path, with every layer
of the stack and the overload bound around them.
A path of routes is served as written, whatever the base path; with
an empty base path they sit next to the product’s routes. Called more
than once, the routes add up.
Without Stack::fallback, a path no route matched takes the
fallback routes declare when they declare one, and the product’s
otherwise, with every layer the product applied. Under an empty base
path that is the product’s own or default fallback; under a non-empty
one only a fallback the product declares, for the paths under the
base path (axum Router::nest, “Fallbacks”), and axum’s empty 404
for the rest.
Sourcepub fn fallback<H, T>(self, handler: H) -> Self
pub fn fallback<H, T>(self, handler: H) -> Self
Answers every request no route matched with handler, inside every
layer of the stack and outside the overload bound.
It answers a path outside the base path and every route beside it,
and a path under the base path that no route matched, unless the
product’s router has a fallback of its own (axum Router::nest,
“Fallbacks”). A second call replaces the first.
Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
Builds the router the listener serves.
§Panics
When axum refuses the routes it merges: a route beside the base path
that is also a route under it, or, with Stack::fallback set, a
router beside or a product under an empty base path that carries a
fallback of its own (axum Router::merge, “Panics”).