Module axum

Web application framework built on tokio and hyper.


axum is a web framework from the tokio project. It follows a typical router + middleware design. It is the preeminent web framework through its association with tokio. The API is async and highly generic, advanced Rust.

Examples

A minimal server:

use axum::{Router, routing::get, extract::Path};
use tokio::net::TcpListener;

async fn hello() -> &'static str {
    "Hello, World!"
}

async fn greet(Path(name): Path<String>) -> String {
    format!("Hello, {name}!")
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(hello))
        .route("/greet/{name}", get(greet));

    let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Handling JSON request and response bodies:

use axum::{Router, routing::post, Json};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
struct CreateItem {
    name: String,
}

#[derive(Serialize)]
struct Item {
    id: u64,
    name: String,
}

async fn create_item(Json(input): Json<CreateItem>) -> Json<Item> {
    Json(Item { id: 1, name: input.name })
}

#[tokio::main]
async fn main() {
    let app = Router::new().route("/items", post(create_item));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Modules