Home / Market / axum development

axum development

Build, structure, and test Rust HTTP services with Axum and Tower. Handle extractor ordering, typed AppState sub-states, IntoResponse errors, middleware layer order, and portless oneshot tests.

skill★ 508220 dlgithubCC-BY-4.0 content; MIT code; upstream exceptions applyInstall in CosmosUpstream Source

Documentation & Preview

--- name: axum-development description: "Build, structure, and test Rust HTTP services with Axum and Tower. Handle extractor ordering, typed AppState sub-states, IntoResponse errors, middleware layer order, and portless oneshot tests." category: development risk: safe source: self source_type: self date_added: "2026-11-30" author: ShianMike tags: [rust, axum, tower, http, backend, testing]

---

Axum Development

When to Use

Prerequisites

Check the Axum major/minor version in Cargo.toml (Axum 0.8 uses /{id} path parameter syntax, replacing the /:id syntax from 0.7), Tokio runtime features, Tower/tower-http middleware versions, and Rust toolchain version. Inspect Router assembly, shared state types, custom error types, and existing tests before adding endpoints or middleware.

Structure routes, extractors, and error responses

  1. Order handler parameters carefully: extractors that read parts of the request (State, Path, Query, HeaderMap) must come before body-consuming extractors (Json, Bytes, String, Form), because only the last extractor may implement FromRequest (consuming the body).
  2. Keep handler dependencies narrow by implementing axum::extract::FromRef<AppState> for sub-states (for example a database pool or limits config). Attach state with .with_state(state) once all stateful routes are merged.
  3. Define a domain AppError enum that implements IntoResponse. Return structured JSON error codes and appropriate HTTP status codes for caller errors (4xx), and log internal errors server-side without leaking stack traces, SQL text, or credentials in 5xx responses.
  4. For Axum 0.8+, use {param} and {*wildcard} route capture syntax; verify migrated routes explicitly when upgrading from 0.7.

Reason about Tower middleware layering order

Test routers in-process and verify shutdown

Example

> Build an Axum 0.8 POST /items/{id} endpoint using FromRef sub-state, a typed AppError response, and a request-ID middleware layer, then test valid, invalid, and malformed requests in-process.

Use the worked Axum handler and middleware test to verify extractor ordering, structured 422 errors, and middleware header injection across success and rejection paths.

Limitations

Safety and Side Effects

Run tests in an isolated crate or checkout without opening public network listeners. Cargo builds may download crates on an uncached machine and execute crate build scripts or proc macros; inspect Cargo.toml and lockfiles before building unfamiliar workspaces. Never include secrets or production database URLs in test fixtures or error responses.

Related Skills and Documentation