
Axum là một web framework Rust hiện đại, được xây dựng trên nền tảng Tokio và Tower, tập trung vào ergonomics (khả năng sử dụng dễ dàng) và modularity (tính mô-đun). Khác với các framework truyền thống có middleware system riêng, Axum tận dụng hệ sinh thái Tower (tower::Service) để cung cấp timeouts, tracing, compression, authorization và nhiều tính năng khác miễn phí.
Điểm mạnh lớn nhất của Axum là macro-free API – routing requests tới handlers không cần macro phức tạp, và declarative request parsing thông qua extractors. Điều này làm cho code dễ đọc, dễ test và dễ maintain hơn so với các framework dùng heavy macro như Actix-web hay Rocket.

Axum cung cấp các extractor mạnh mẽ để parse request:
- Path: Extract route parameters (VD:
/users/:id) - Query: Parse query string thành struct
- Json: Deserialize JSON body vào struct (sử dụng serde)
- Form: Parse form data
- Header: Extract headers cụ thể
- Extension: Truy cập shared state (database pool, config)
- State: Type-safe shared application state
Error handling trong Axum đơn giản và predictable: handler trả về Result với E: IntoResponse. Điều này cho phép chuyển đổi error thành HTTP response tự động, bao gồm status code, headers và body.

Ví dụ cơ bản về Axum application:
use axum::{
routing::{get, post},
http::StatusCode,
Json, Router, Extension,
};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
#[tokio::main]
async fn main() {
let pool = PgPool::connect(&database_url).await.unwrap();
let app = Router::new()
.route("/", get(root))
.route("/users", post(create_user))
.layer(Extension(pool));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn root() -> &'static str {
"Hello, World!"
}
async fn create_user(
Extension(pool): Extension,
Json(payload): Json,
) -> Result<(StatusCode, Json), AppError> {
let user = sqlx::query_as!(User, "INSERT ...")
.fetch_one(&pool)
.await?;
Ok((StatusCode::CREATED, Json(user)))
}
Performance của Axum ngang ngửa với hyper (mà nó xây dựng trên) vì Axum là thin layer. Các benchmark cho thấy Axum đứng top trong Rust web frameworks về throughput và latency.
Tính năng nổi bật của Axum 0.7+:
- State management: Type-safe shared state với
ExtensionvàState - WebSocket support: Built-in WebSocket upgrade handling
- Multipart forms: Stream multipart uploads
- Typed Header: Compile-time checked header extraction
- Graceful shutdown: Tích hợp với tower::util::ServiceExt

So sánh nhanh với các framework Rust khác:
| Framework | Middleware | Macro usage | Ecosystem |
|---|---|---|---|
| Axum | Tower (composable) | Minimal | Tokio + Tower + Hyper |
| Actix-web | Custom actor system | Heavy | Actor-based |
| Rocket | Custom fairings | Heavy | Opinionated |
| Warp | Filter composition | Heavy | Filter-based |
Axum phù hợp cho: microservices, REST APIs, GraphQL servers, real-time apps với WebSocket, và high-performance backends cần type safety và zero-cost abstractions của Rust.
Tham khảo thêm: Axum GitHub Repository | Axum Documentation | Axum on crates.io
