Ergonomic and modular web framework built with Tokio, Tower, and Hyper https://crates.io/crates/axum
Find a file
2024-10-18 21:04:24 +02:00
.github Use explicit CI runner version (#2996) 2024-10-18 21:04:24 +02:00
axum fix(sse): skip sse incompatible chars of serde_json::RawValue (#2992) 2024-10-17 16:49:39 +02:00
axum-core Remove versions from internal dev-dependencies 2024-10-05 11:44:31 +00:00
axum-extra Fix TSR redirecting to top-level inside nested Router (#2993) 2024-10-17 16:43:14 +02:00
axum-macros Remove versions from internal dev-dependencies 2024-10-05 11:44:31 +00:00
examples Remove unneeded macro usage (#2995) 2024-10-17 16:47:51 +02:00
.gitignore Re-integrate examples into the root workspace (#1466) 2023-03-10 12:02:11 +01:00
.typos.toml Check for typos in CI 2023-08-08 11:27:45 +02:00
_typos.toml Update to latest versions of hyper and http-body (#1882) 2023-11-23 11:03:03 +00:00
Cargo.toml Update MSRV for all crates to 1.75 (#2943) 2024-09-28 21:21:41 +00:00
CHANGELOG.md Move axum crate into workspace subfolder (#458) 2021-11-03 12:38:48 +01:00
clippy.toml Require Sync for services (#2473) 2024-09-28 23:28:44 +02:00
CONTRIBUTING.md Fix typo in CONTRIBUTING.md (#2612) 2024-02-25 21:47:01 +01:00
deny.toml ci: Add more allowances for multiple version of the same crate 2024-09-20 20:34:07 +02:00
ECOSYSTEM.md ECOSYSTEM.md: Add Pods-Blitz (#2983) 2024-10-14 04:21:02 -04:00
README.md Move axum crate into workspace subfolder (#458) 2021-11-03 12:38:48 +01:00

axum

axum is a web application framework 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

  • Route requests to handlers with a macro free API.
  • Declaratively parse requests using extractors.
  • Simple and predictable error handling model.
  • Generate responses with minimal boilerplate.
  • Take full advantage of the tower and tower-http ecosystem of middleware, services, and utilities.

In particular the last point is what sets axum apart from other 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.8 so the main branch contains breaking changes. See the 0.7.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.unwrap();
}

// 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.75.

Examples

The examples folder contains various examples of how to use axum. The docs also provide lots of code snippets and examples. For full-fledged examples, check out community-maintained showcases or tutorials.

Getting Help

In the axum's repo we also have a number of examples showing how to put everything together. Community-maintained showcases and tutorials also demonstrate how to use axum for real-world applications. You're also welcome to ask in the Discord channel or open a discussion with your question.

Community projects

See here for a list of community maintained crates and projects built with axum.

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.