what about typed methods?

This commit is contained in:
David Pedersen
2022-02-18 11:53:42 +01:00
parent d3bcf7778d
commit cc1f989467
5 changed files with 337 additions and 283 deletions
-3
View File
@@ -7,6 +7,3 @@ publish = false
[dependencies]
axum = { path = "../../axum" }
tokio = { version = "1.0", features = ["full"] }
axum-extra = { path = "../../axum-extra", features = ["typed-routing"] }
serde = { version = "1.0", features = ["derive"] }
+10 -39
View File
@@ -4,52 +4,23 @@
//! cargo run -p example-hello-world
//! ```
// Just using this file for manual testing. Will be cleaned up before an eventual merge
use axum::{response::IntoResponse, Router};
use axum_extra::routing::{RouterExt, TypedPath};
use serde::Deserialize;
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new()
.typed_get(users_index)
.typed_post(users_create)
.typed_get(users_show)
.typed_get(users_edit);
// build our application with a route
let app = Router::new().route("/", get(handler));
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
#[derive(TypedPath)]
#[typed_path("/users")]
struct UsersCollection;
#[derive(Deserialize, TypedPath)]
#[typed_path("/users/:id")]
struct UsersMember {
id: u32,
}
#[derive(Deserialize, TypedPath)]
#[typed_path("/users/:id/edit")]
struct UsersEdit(u32);
async fn users_index(_: UsersCollection) -> impl IntoResponse {
"users#index"
}
async fn users_create(_: UsersCollection, _payload: String) -> impl IntoResponse {
"users#create"
}
async fn users_show(UsersMember { id }: UsersMember) -> impl IntoResponse {
format!("users#show: {}", id)
}
async fn users_edit(UsersEdit(id): UsersEdit) -> impl IntoResponse {
format!("users#edit: {}", id)
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
+4 -1
View File
@@ -37,7 +37,10 @@ async fn main() {
let app = Router::new()
.fallback(static_files_service)
.route("/sse", get(sse_handler))
.layer(TraceLayer::new_for_http());
.layer(
TraceLayer::new_for_http()
.make_span_with(tower_http::trace::DefaultMakeSpan::new().include_headers(true)),
);
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));