axum-middleware

Compose Axum middleware with correct ServiceBuilder layering and route_layer usage.

Updated May 26, 2026
One-click install
npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill axum-middleware
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: axum-middleware
Source: https://github.com/adelabdelgawad/rust-fullstack-agents/tree/main/plugins/rusty/skills/axum-middleware
Command: npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill axum-middleware

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Building Axum servers often suffers from misordered middleware, confusion between route-layer and global layers, and fragile shutdown handling. This guide explains how to structure middleware to ensure predictable behavior and safe shutdown.

Core Features & Use Cases

  • Correct ServiceBuilder layering for multiple middlewares.
  • Clear distinction and usage guidance for route_layer vs layer.
  • Safe, graceful shutdown integration and testing tips.

Quick Start

Add the recommended middleware stack to your Axum router and verify the behavior across routes.

Frequently Asked Questions about axum-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I order middleware correctly in an Axum ServiceBuilder?

To order Axum middleware correctly, structure your ServiceBuilder layers so that layers added last wrap the outermost request handling. This enforces predictable middleware execution and prevents fragile server behavior.

What is the difference between route_layer and layer in Axum?

The difference between route_layer and layer in Axum is application scope: route_layer applies middleware to specific matched routes, while layer applies it globally. Proper usage ensures per-route vs global middleware isolation.

How do I pass application state to Axum middleware using from_fn_with_state?

You pass application state to Axum middleware using the from_fn_with_state function, which accepts your state instance alongside the middleware logic, enabling safe access to shared data within your middleware functions.

Does Axum middleware work with tower-http layers?

Yes, Axum middleware works with tower-http layers. The framework ensures compatibility with tower-http layers when composed correctly within the ServiceBuilder, allowing you to add standard HTTP utilities seamlessly.

How do I integrate graceful shutdown with an Axum middleware stack?

You integrate graceful shutdown with an Axum middleware stack by configuring the server's shutdown signal alongside your ServiceBuilder layering. This prevents dropping active requests during server termination.

Why is my Axum middleware executing in the wrong order?

Axum middleware executes in the wrong order when ServiceBuilder layering is misconfigured or when route_layer and layer are confused. Enforcing correct layering per the guidance ensures requests and responses pass through predictably.