Web services with axum
9 steps in 4 sets of Rust.
A small web service in Rust. Routes and handlers, then extractors and shared state, which is where axum's type-driven design either delights you or produces a compiler error the length of a page.
Responses and middleware close it out. Nine steps, deliberately narrow: enough to see the shape of an axum service without pretending a tour can teach you to run one in production.
Routes & handlers
- A router and a handlerAn axum service is a Router of routes, each pointing at an async handler.
- JSON in and outJson is both an extractor and a response, driven by serde on your types.
- Nesting and fallbacksRouters compose: nest a sub-router under a prefix and give the rest a fallback.
Extractors & state
- Path and Query extractorsTyped extractors pull the parts of a URL apart for you.
- Shared stateState carries a handle to whatever your handlers share — a pool, a cache, a counter.
Responses & middleware
- Status codes and headersA tuple response sets the status, the headers and the body in one return.
- An error type that respondsImplement IntoResponse for your error and handlers can use the ? operator.
- Middleware layersA layer wraps every route below it: logging, timeouts, compression.
Encore
- tour_api.rsA whole small service: shared state, nested routes, typed errors, JSON replies.