Skip to main content

Stack

Struct Stack 

Source
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, &section, StackPolicy::default())
    .beside(fhir)
    .fallback(|| async { (StatusCode::NOT_FOUND, "no such resource") })
    .into_router();

Implementations§

Source§

impl Stack

Source

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.

Source

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.

Source

pub fn fallback<H, T>(self, handler: H) -> Self
where H: Handler<T, ()>, T: 'static,

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.

Source

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”).

Trait Implementations§

Source§

impl Debug for Stack

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Stack

§

impl !UnwindSafe for Stack

§

impl Forget for Stack

§

impl Freeze for Stack

§

impl Move for Stack

§

impl Send for Stack

§

impl Sync for Stack

§

impl Unpin for Stack

§

impl UnsafeUnpin for Stack

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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