All topics / axum From Zero

axum From Zero

Learn the Tokio team's Rust web framework — the one this very platform runs on: the router and your first server, routing and extractors, handlers and IntoResponse, shared state, middleware with Tower, a full REST API, error handling that uses Rust's type system, and testing and production. Built on hyper and tower, ergonomic without macros.

  1. What axum Is & Your First Server axum maps paths to plain async fn handlers and turns their return values into responses — no macros. Install it, write a first server on Tokio, run it, and meet the Router and the books API.
  2. Routing & Extractors Routes are method + path → handler, and a handler's parameters are extractors that pull typed data from the request. Path, Query, and nesting routers for versioned APIs.
  3. Handlers & IntoResponse How axum handlers really work: arguments extract from the request, the return value becomes the response. Json in and out, IntoResponse, status codes, and reading the trait-bound error.
  4. Shared State How axum hands shared dependencies — a store, a config, a database pool — to stateless handlers via State<T> and with_state, why the state type must be Clone, and why mutable data lives behind Arc<Mutex>.
  5. Middleware with Tower Middleware in axum is a tower Layer wrapping your whole service. Add ready-made tower-http layers, stack them with ServiceBuilder, write custom middleware with from_fn, and master layer ordering.
  6. Building a REST API Wire the books API into full CRUD — five async handlers over one shared store, each composing State, Path, and Json, returning status codes and JSON through IntoResponse.
  7. Error Handling A custom AppError type that implements IntoResponse turns errors into return values: handlers return Result, the ? operator propagates and converts them, and From impls fold foreign errors into one JSON shape.
  8. Testing & Production Test an axum app in memory with tower's oneshot (no ports), structure it as a fn app() shared by main and tests, then ship it with graceful shutdown, a distroless container, env config, and real tracing logs.
  9. Where to Go Next You can build, layer, test, and ship a real axum API. Now the honest map: axum vs actix/Rocket, the data layer (sqlx/SeaORM/Diesel), the tokio/hyper/tower roots, and what to build.