Crates.io | zhou |
lib.rs | zhou |
version | 0.1.2 |
source | src |
created_at | 2021-07-13 06:20:51.541237 |
updated_at | 2022-09-10 12:57:41.606951 |
description | A short description of my package |
homepage | https://serde.rs/ |
repository | https://github.com/mojocn/zhou |
max_upload_size | |
id | 422180 |
size | 95,204 |
HTTP1.1
HTTP2
HTTP2
https://github.dev/hyperium/hyper
https://github.dev/hyperium/http
https://github.com/gin-gonic/gin#asciijson
axum
is a web application framework that focuses on ergonomics and modularity.
More information about this crate can be found in the crate documentation.
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
.
We are currently working towards axum 0.6 so the main
branch contains breaking
changes. See the 0.5.x
branch for whats released to crates.io and up to date
changelogs.
use axum::{
routing::{get, post},
http::StatusCode,
response::IntoResponse,
Json, Router,
};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
#[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
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.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>,
) -> impl IntoResponse {
// 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.
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.
This crate uses #![forbid(unsafe_code)]
to ensure everything is implemented in
100% safe Rust.
axum's MSRV is 1.60.
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.
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 an issue with your question.
See here for a list of community maintained crates and projects
built with axum
.
:balloon: 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.
This project is licensed under the MIT license.
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.