2021-11-03 12:38:48 +01:00
# axum
`axum` is a web application framework that focuses on ergonomics and modularity.
[](https://github.com/tokio-rs/axum/actions/workflows/CI.yml)
[](https://crates.io/crates/axum)
[](https://docs.rs/axum)
More information about this crate can be found in the [crate documentation][docs].
## 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` ].
2024-09-11 20:42:15 +02:00
## 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 and up to
date changelogs.
2021-11-03 12:38:48 +01:00
## Usage example
```rust
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 ));
2023-05-03 14:17:18 -07:00
// run our app with hyper, listening globally on port 3000
2023-03-22 23:42:14 +01:00
let listener = tokio ::net ::TcpListener ::bind ( "0.0.0.0:3000" ). await . unwrap ();
axum ::serve ( listener , app ). await . unwrap ();
2021-11-03 12:38:48 +01:00
}
// 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 > ,
2023-02-07 17:57:28 +01:00
) -> ( StatusCode , Json < User > ) {
2021-11-03 12:38:48 +01:00
// 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][readme-example] as well as other example projects in
the [example directory][examples].
See the [crate documentation][docs] for way more examples.
## Performance
`axum` is a relatively thin layer on top of [`hyper` ] and adds very little
2021-12-29 23:02:37 +01:00
overhead. So `axum` 's performance is comparable to [`hyper` ]. You can find
benchmarks [here ](https://github.com/programatik29/rust-web-benchmarks ) and
[here ](https://web-frameworks-benchmark.netlify.app/result?l=rust ).
2021-11-03 12:38:48 +01:00
## Safety
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in
100% safe Rust.
## Minimum supported Rust version
2023-11-23 12:03:03 +01:00
axum's MSRV is 1.66.
2021-11-03 12:38:48 +01:00
## Examples
The [examples] folder contains various examples of how to use `axum` . The
2022-04-24 12:49:15 +02:00
[docs] also provide lots of code snippets and examples. For full-fledged examples, check out community-maintained [showcases] or [tutorials].
2021-11-03 12:38:48 +01:00
## Getting Help
In the `axum` 's repo we also have a [number of examples][examples] showing how
2022-12-09 10:14:31 +01:00
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][chat] or open a [discussion] with your question.
2021-11-03 12:38:48 +01:00
## Community projects
2021-11-04 19:57:05 +01:00
See [here][ecosystem] for a list of community maintained crates and projects
2022-04-24 12:49:15 +02:00
built with `axum` .
2021-11-03 12:38:48 +01:00
## Contributing
2023-08-03 10:23:18 +02:00
🎈 Thanks for your help improving the project! We are so happy to have
2021-11-03 12:38:48 +01:00
you! We have a [contributing guide][contributing] to help you get involved in the
`axum` project.
## License
2021-11-04 19:57:05 +01:00
This project is licensed under the [MIT license][license].
2021-11-03 12:38:48 +01:00
### 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.
[readme-example]: https://github.com/tokio-rs/axum/tree/main/examples/readme
[examples]: https://github.com/tokio-rs/axum/tree/main/examples
[docs]: https://docs.rs/axum
[`tower` ]: https://crates.io/crates/tower
[`hyper` ]: https://crates.io/crates/hyper
[`tower-http` ]: https://crates.io/crates/tower-http
[`tonic` ]: https://crates.io/crates/tonic
2022-01-31 19:09:55 +03:00
[contributing]: https://github.com/tokio-rs/axum/blob/main/CONTRIBUTING.md
2021-11-03 12:38:48 +01:00
[chat]: https://discord.gg/tokio
2022-12-09 10:14:31 +01:00
[discussion]: https://github.com/tokio-rs/axum/discussions/new?category=q-a
2021-11-03 12:38:48 +01:00
[`tower::Service` ]: https://docs.rs/tower/latest/tower/trait.Service.html
2022-01-31 19:09:55 +03:00
[ecosystem]: https://github.com/tokio-rs/axum/blob/main/ECOSYSTEM.md
2022-04-24 12:49:15 +02:00
[showcases]: https://github.com/tokio-rs/axum/blob/main/ECOSYSTEM.md#project -showcase
[tutorials]: https://github.com/tokio-rs/axum/blob/main/ECOSYSTEM.md#tutorials
2022-01-31 19:09:55 +03:00
[license]: https://github.com/tokio-rs/axum/blob/main/axum/LICENSE