X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=axum-server%2Fsrc%2Fmain.rs;fp=axum-server%2Fsrc%2Fmain.rs;h=1225f67614184b5b34cc0bb3a9703bb19d225433;hp=aa2a26337a2551d44d1a77d3cc598214d97fffa0;hb=c9927542defc4cf63e714aef077c84598e48ab00;hpb=d154738629ef14b2141a01aac34a7b39c263eed1 diff --git a/axum-server/src/main.rs b/axum-server/src/main.rs index aa2a263..1225f67 100644 --- a/axum-server/src/main.rs +++ b/axum-server/src/main.rs @@ -1,12 +1,14 @@ +use std::io; + use axum::{ - routing::{get}, + routing::{get, get_service}, extract::Path, - http, + http::{StatusCode}, response, Router}; async fn hello(Path(name): Path) -> impl response::IntoResponse { - (http::StatusCode::OK, format!("Hello {}", name)) + (StatusCode::OK, format!("Hello {}", name)) } #[tokio::main] @@ -14,6 +16,12 @@ async fn main() { tracing_subscriber::fmt::init(); let app = Router::new() .route("/hello/:name", get(hello)) + .route("/static", + get_service(tower_http::services::ServeFile::new("Cargo.toml").precompressed_gzip()) + .handle_error(|e: io::Error| async move { + (StatusCode::INTERNAL_SERVER_ERROR, format!("Unhandled internal error: {}", e)) + }) + ) .layer( tower::ServiceBuilder::new() .layer(tower_http::trace::TraceLayer::new_for_http())