]> piware.de Git - learn-rust.git/commitdiff
axum-server: Initial hello world
authorMartin Pitt <martin@piware.de>
Sat, 12 Nov 2022 06:35:33 +0000 (07:35 +0100)
committerMartin Pitt <martin@piware.de>
Sat, 12 Nov 2022 06:35:33 +0000 (07:35 +0100)
axum-server/Cargo.toml [new file with mode: 0644]
axum-server/src/main.rs [new file with mode: 0644]

diff --git a/axum-server/Cargo.toml b/axum-server/Cargo.toml
new file mode 100644 (file)
index 0000000..206a653
--- /dev/null
@@ -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 (file)
index 0000000..aa2a263
--- /dev/null
@@ -0,0 +1,28 @@
+use axum::{
+    routing::{get},
+    extract::Path,
+    http,
+    response,
+    Router};
+
+async fn hello(Path(name): Path<String>) -> 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();
+}