liveBook · Manning

Axum

As explained in Code Like a Pro in Rust

axum is a Rust web framework for building HTTP server applications and routing requests to handlers.

Axum is a Rust framework for HTTP server APIs on the Tokio async runtime. Its small, mostly macro-free core provides routing, request extractors, responses, and error handling without imposing a service architecture. It builds on Tower and hyper; deeper service customization calls for Tower knowledge. section 9.1 section 9.11

Start with `cargo add axum`, then add crates for the capabilities the service needs. A SQLite-backed API can combine SQLx for persistence, Serde for serialization, and `tower-http` for tracing and CORS. Rust’s type checking helps catch mistakes when these components are assembled. section 9.4

Declare API routes

Use `Router::new()` to associate paths and HTTP methods with handlers: ```rust Router::new() .route("/alive", get(|| async { "ok" })) .route("/ready", get(ping)) .nest( "/v1", Router::new() .route("/todos", get(todo_list).post(todo_create)) .route( "/todos/:id", get(todo_read).put(todo_update).delete(todo_delete), ), ) ``` The router can also receive handler state and layers such as tracing and CORS. Keep route declarations aligned with the API design; consult Axum’s documentation for features beyond this basic pattern. section 9.7

Table 9.3 API service dependencies (view table figure)

Name

Features

Description

axum

Default

Web framework

chrono

serde

Date/time library, with serde feature

serde

derive

Serialization/deserialization library, with #[derive(...)] feature

serde_json

Default

JSON serialization/deserialization for the serde crate

sqlx

runtime-tokio-rustls, sqlite,chrono,macros

Async SQL toolkit for SQLite, MySQL, and PostgreSQL

tokio

macros,rt-multi-thread

Async runtime, used with axum and sqlx

tower-http

trace,cors

Provides HTTP middleware for axum, specifically, tracing and CORS

tracing

default

Async tracing library

tracing-subscriber

env-filter

Allows us to subscribe to tracing data within crates that use tracing

The dependency table identifies which capabilities come from Axum and which require companion crates and features. section 9.4

Use Axum for the server-facing HTTP API rather than working directly with hyper’s lower-level server interface. Add tracing and metrics for production needs, and choose data modeling, state management, and response formats explicitly: the framework does not make those design decisions for you. section 10.8 section 9.11

How Code Like a Pro in Rust covers it

How deep it goes
Shows dependency setup and Router declarations, but defers deeper framework features to Axum documentation.
Examples
Uses health-check routes and versioned todo endpoints with GET, POST, PUT, and DELETE handlers.
What it assumes
Assumes familiarity with Rust and Cargo while demonstrating how to add crates and declare API routes.
What you can do after
Build it from scratch as an Axum API router, combining handlers with the dependencies and layers a service needs.
How it connects
Connects routing and middleware to Tokio, Tower, hyper, SQLx, Serde, tracing, and CORS.

Also explained in

Compare how 2 books explain Axum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.