axum-impl-middleware
Installation
SKILL.md
axum-impl-middleware
Overview
Axum has no middleware system of its own. It integrates the Tower ecosystem
(tower::Service plus tower::Layer). The axum::middleware module supplies
ergonomic helpers so middleware can be a plain async fn instead of a
hand-written Service.
Three implementation strategies, narrowest first:
map_request/map_response: a pure one-way transform (add a header, normalise a field). NoNext, cannot short-circuit, cannot see both sides.from_fn/from_fn_with_state: anasync fnthat receivesNextand runs code before AND after the handler, and may short-circuit it.- A hand-written
tower::Layerplustower::Service: required only when the middleware must be configurable or published as a crate.