Skip to main content

Module middleware

Module middleware 

Source
Expand description

Request-lifecycle middleware utilities and Tower layer factories.

This module provides utilities that operate at request time, not at route registration time. These are Tower layer factories — they produce middleware that wraps individual routes or entire routers.

§Module Boundaries

  • pipeline.rs — build-time: compose routes into a Router
  • controller.rs — build-time: declare which handlers belong to a controller
  • middleware.rs — request-time: inspect/modify requests and responses

§Protected Route Groups

Auth is a cross-cutting concern — it belongs here as a router transform, not inside a controller handler. Apply it to a route group via .map():

RouterPipeline::new()
    .mount_guarded::<AdminController, _>(admin_svc, || { /* config check */ })
    .map(require_bearer(admin_key))

Or scoped to just a sub-group:

RouterPipeline::new()
    .group("/admin", |g| g
        .mount::<AdminController>(admin_svc)
        .map(require_bearer(admin_key))   // only admin routes are protected
    )

Functions§

guard
Returns a Router -> Router transform that guards every request with a predicate.
require_bearer
Returns a Router -> Router transform that enforces Authorization: Bearer <token> on every request passing through the router it is applied to.