axum README

The project's README from github.com/tokio-rs/axum. As one markdown file: README.md.

axum

axum is an HTTP routing and request-handling library that focuses on ergonomics and modularity.

Build status Crates.io Documentation

More information about this crate can be found in the crate documentation.

High level features

In particular the last point is what sets axum apart from other libraries / frameworks. axum doesn't have its own middleware system but instead uses tower::Service. This means axum gets timeouts, tracing, compression, authorization, and more, for free. It also enables you to share middleware with applications written using hyper or tonic.

⚠ Breaking changes ⚠

We are currently working towards axum 0.9 so the main branch contains breaking changes. See the 0.8.x branch for what's released to crates.io.

Usage example

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

#[tokio::main]
async fn main() {
    // initialize tracing
    tracing_subscriber::fmt::init();

    // build our application with a route
    let app = Router::new()
        // `GET /` goes to `root`
        .route("/", get(root))
        // `POST /users` goes to `create_user`
        .route("/users", post(create_user));

    // run our app with hyper, listening globally on port 3000
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await;
}

// basic handler that responds with a static string
async fn root() -> &'static str {
    "Hello, World!"
}

async fn create_user(
    // this argument tells axum to parse the request body
    // as JSON into a `CreateUser` type
    Json(payload): Json<CreateUser>,
) -> (StatusCode, Json<User>) {
    // insert your application logic here
    let user = User {
        id: 1337,
        username: payload.username,
    };

    // this will be converted into a JSON response
    // with a status code of `201 Created`
    (StatusCode::CREATED, Json(user))
}

// the input to our `create_user` handler
#[derive(Deserialize)]
struct CreateUser {
    username: String,
}

// the output to our `create_user` handler
#[derive(Serialize)]
struct User {
    id: u64,
    username: String,
}

You can find this example as well as other example projects in the example directory.

See the crate documentation for way more examples.

Performance

axum is a relatively thin layer on top of hyper and adds very little overhead. So axum's performance is comparable to hyper. You can find benchmarks here and here.

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

Minimum supported Rust version

axum's MSRV is 1.80.

Examples

The examples folder contains various examples of how to use axum. The docs also provide lots of code snippets and examples.

Getting Help

In the axum's repo we also have a number of examples showing how to put everything together. You're also welcome to ask in the Discord channel or open a discussion with your question.

Contributing

🎈 Thanks for your help improving the project! We are so happy to have you! We have a contributing guide to help you get involved in the axum project.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in axum by you, shall be licensed as MIT, without any additional terms or conditions.

axum (tokio-rs/axum)

Searching Database…

27.5K ·GitHub
What is this?axum is an HTTP routing and request-handling library for Rust with a focus on ergonomics and modularity — the kind of crate you build web services and APIs with.
Is it a full web framework or a thin layer over something else?It's a library, not a standalone app: you add it to a Rust project, and it routes requests to your handler functions as a thin layer on top of the hyper HTTP implementation.
How do I add it to a Rust project?Add it as a Cargo dependency (cargo add axum). The repo's examples folder holds full example projects, and the crate docs on docs.rs have many more code snippets.
What do I need installed before I can use it?Just a Rust toolchain — the minimum supported Rust version is 1.80. Everything else (hyper, tower) arrives as ordinary dependencies.
Is there anything tricky about picking a version right now?Yes — the main branch is mid-way to 0.9 and contains breaking changes; stable releases live on the 0.8.x branch of crates.io, so pin your dependency to a released version.
Is the license fine for commercial use?Yes — it's MIT, a permissive license. Contributions are also licensed MIT unless you explicitly state otherwise.
What sets axum apart from other Rust web libraries?It has no middleware system of its own — it plugs into tower::Service instead, so timeouts, tracing, compression and authorization come for free, and middleware can be shared with apps written on hyper or tonic.
What does handler code look like — do I need macros?No macros at all: routing uses a macro-free API. Handlers pull request data declaratively via extractors, errors follow a simple and predictable model, and responses take minimal boilerplate.
Is it fast?Yes — the README notes performance is comparable to hyper, since axum adds very little overhead on top of it, and links to published benchmarks.
Is the code written in safe Rust?Yes — the crate uses #![forbid(unsafe_code)], so everything is implemented in 100% safe Rust.
What are similar projects?martini, Rocket, starlette, hyper, express, mux, koa, sinatra
Creator
tokio-rs
Size
6 MB
Stars
27.5K
Age
5y
Commits · 90 Days