mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Update readme example to 0.2
This commit is contained in:
@@ -36,7 +36,12 @@ below still uses the 0.1 API.
|
|||||||
## Usage example
|
## Usage example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use axum::{prelude::*, response::IntoResponse, http::StatusCode};
|
use axum::{
|
||||||
|
handler::{get, post},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
@@ -46,9 +51,9 @@ async fn main() {
|
|||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app =
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
// `GET /` goes to `root`
|
||||||
route("/", get(root))
|
.route("/", get(root))
|
||||||
// `POST /users` goes to `create_user`
|
// `POST /users` goes to `create_user`
|
||||||
.route("/users", post(create_user));
|
.route("/users", post(create_user));
|
||||||
|
|
||||||
@@ -70,7 +75,7 @@ async fn root() -> &'static str {
|
|||||||
async fn create_user(
|
async fn create_user(
|
||||||
// this argument tells axum to parse the request body
|
// this argument tells axum to parse the request body
|
||||||
// as JSON into a `CreateUser` type
|
// as JSON into a `CreateUser` type
|
||||||
extract::Json(payload): extract::Json<CreateUser>,
|
Json(payload): Json<CreateUser>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
// insert your application logic here
|
// insert your application logic here
|
||||||
let user = User {
|
let user = User {
|
||||||
@@ -80,7 +85,7 @@ async fn create_user(
|
|||||||
|
|
||||||
// this will be converted into a JSON response
|
// this will be converted into a JSON response
|
||||||
// with a status code of `201 Created`
|
// with a status code of `201 Created`
|
||||||
(StatusCode::CREATED, response::Json(user))
|
(StatusCode::CREATED, Json(user))
|
||||||
}
|
}
|
||||||
|
|
||||||
// the input to our `create_user` handler
|
// the input to our `create_user` handler
|
||||||
|
|||||||
Reference in New Issue
Block a user