From 36b6c6242182cda9ec14ef73683a26be08148deb Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 5 Jul 2024 17:19:00 +0100 Subject: [PATCH] WIP: New example setting a header read timeout --- examples/header_read_timeout.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/header_read_timeout.rs diff --git a/examples/header_read_timeout.rs b/examples/header_read_timeout.rs new file mode 100644 index 00000000..4ecdf508 --- /dev/null +++ b/examples/header_read_timeout.rs @@ -0,0 +1,29 @@ +//! Run with `cargo run --example header_read_timeout` command. +//! +//! To connect through browser, navigate to "http://localhost:3000" url. + +use axum::{routing::get, Router}; +use hyper_util::rt::TokioTimer; +use std::net::{SocketAddr, TcpListener}; +use std::time::Duration; + +#[tokio::main] +async fn main() { + let app = Router::new().route("/", get(|| async { "Hello, world!" })); + + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + + let listener = TcpListener::bind(addr).unwrap(); + + println!("listening on {}", addr); + + let mut server = axum_server::from_tcp(listener); + + server.http_builder().http1().timer(TokioTimer::new()); + server + .http_builder() + .http1() + .header_read_timeout(Duration::from_secs(5)); + + server.serve(app.into_make_service()).await.unwrap(); +}