From: Martin Pitt Date: Sat, 12 Nov 2022 06:35:33 +0000 (+0100) Subject: axum-server: Initial hello world X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=commitdiff_plain;h=d154738629ef14b2141a01aac34a7b39c263eed1 axum-server: Initial hello world --- diff --git a/axum-server/Cargo.toml b/axum-server/Cargo.toml new file mode 100644 index 0000000..206a653 --- /dev/null +++ b/axum-server/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "axum-server" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +axum = "0.5" +tokio = { version = "1", features = ["full"] } +tower = "0.4" +tower-http = { version = "0.3", features = ["trace"] } +tracing = "0.1" +tracing-subscriber = "0.3" diff --git a/axum-server/src/main.rs b/axum-server/src/main.rs new file mode 100644 index 0000000..aa2a263 --- /dev/null +++ b/axum-server/src/main.rs @@ -0,0 +1,28 @@ +use axum::{ + routing::{get}, + extract::Path, + http, + response, + Router}; + +async fn hello(Path(name): Path) -> impl response::IntoResponse { + (http::StatusCode::OK, format!("Hello {}", name)) +} + +#[tokio::main] +async fn main() { + tracing_subscriber::fmt::init(); + let app = Router::new() + .route("/hello/:name", get(hello)) + .layer( + tower::ServiceBuilder::new() + .layer(tower_http::trace::TraceLayer::new_for_http()) + ); + + let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000)); + tracing::info!("listening on {}", addr); + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); +}