]> piware.de Git - learn-rust.git/blobdiff - axum-server/src/main.rs
axum-server: Initial hello world
[learn-rust.git] / axum-server / src / main.rs
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();
+}