]> piware.de Git - learn-rust.git/blob - axum-server/src/main.rs
axum-server: Initial hello world
[learn-rust.git] / axum-server / src / main.rs
1 use axum::{
2     routing::{get},
3     extract::Path,
4     http,
5     response,
6     Router};
7
8 async fn hello(Path(name): Path<String>) -> impl response::IntoResponse {
9     (http::StatusCode::OK, format!("Hello {}", name))
10 }
11
12 #[tokio::main]
13 async fn main() {
14     tracing_subscriber::fmt::init();
15     let app = Router::new()
16         .route("/hello/:name", get(hello))
17         .layer(
18             tower::ServiceBuilder::new()
19                 .layer(tower_http::trace::TraceLayer::new_for_http())
20         );
21
22     let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
23     tracing::info!("listening on {}", addr);
24     axum::Server::bind(&addr)
25         .serve(app.into_make_service())
26         .await
27         .unwrap();
28 }