From 7540310b2995c11b22def50fad989a789b80db36 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Mon, 27 Nov 2023 08:54:10 -0800 Subject: [PATCH] Bump axum to 0.7.1 (#21) --- Cargo.toml | 5 +++-- README.md | 16 +++++++++------- examples/insecure.rs | 16 +++++++++------- examples/secure.rs | 13 +++++++++---- src/insecure.rs | 12 ++++-------- src/lib.rs | 16 +++++++++------- src/rudimental.rs | 7 ++++--- 7 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e086c54..fbf7951 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,13 +7,14 @@ repository = "https://github.com/imbolc/axum-client-ip" version = "0.4.2" [dependencies] -axum = { version = "0.6", default-features = false, features = ["http1", "tokio"] } +axum = { version = "0.7.1", default-features = false, features = ["http1", "tokio"] } forwarded-header-value = "0.1" serde = { version = "1", features = ["derive"] } [dev-dependencies] envy = "0.4" -hyper = "0.14" +hyper = "1.0" rusty-hook = "0.11" tokio = { version = "1", features = ["full"] } tower = "0.4" +http-body-util = "0.1" diff --git a/README.md b/README.md index 3b20241..72a89b6 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,15 @@ async fn main() { let app = Router::new().route("/", get(handler)) .layer(SecureClientIpSource::ConnectInfo.into_extension()); - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) - .serve( - // Don't forget to add `ConnectInfo` if you aren't behind a proxy - app.into_make_service_with_connect_info::() - ) - .await - .unwrap() + let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); + axum::serve( + listener, + // Don't forget to add `ConnectInfo` if you aren't behind a proxy + app.into_make_service_with_connect_info::(), + ) + .await + .unwrap() } ``` diff --git a/examples/insecure.rs b/examples/insecure.rs index a63480b..98368a7 100644 --- a/examples/insecure.rs +++ b/examples/insecure.rs @@ -10,11 +10,13 @@ async fn handler(InsecureClientIp(ip): InsecureClientIp) -> String { async fn main() { let app = Router::new().route("/", get(handler)); - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) - .serve( - // Don't forget to add `ConnectInfo` - app.into_make_service_with_connect_info::(), - ) - .await - .unwrap() + let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); + axum::serve( + listener, + // Don't forget to add `ConnectInfo` + app.into_make_service_with_connect_info::(), + ) + .await + .unwrap() } diff --git a/examples/secure.rs b/examples/secure.rs index 780db90..85f6ce3 100644 --- a/examples/secure.rs +++ b/examples/secure.rs @@ -22,8 +22,13 @@ async fn main() { // the line you're probably looking for :) .layer(config.ip_source.into_extension()); - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) - .serve(app.into_make_service_with_connect_info::()) - .await - .unwrap() + let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); + axum::serve( + listener, + // Don't forget to add `ConnectInfo` + app.into_make_service_with_connect_info::(), + ) + .await + .unwrap() } diff --git a/src/insecure.rs b/src/insecure.rs index 0467049..5e87fb9 100644 --- a/src/insecure.rs +++ b/src/insecure.rs @@ -80,12 +80,8 @@ fn maybe_connect_info(extensions: &Extensions) -> Option { #[cfg(test)] mod tests { use super::InsecureClientIp; - use axum::{ - body::{Body, BoxBody}, - http::Request, - routing::get, - Router, - }; + use axum::{body::Body, http::Request, routing::get, Router}; + use http_body_util::BodyExt; use tower::ServiceExt; fn app() -> Router { @@ -95,8 +91,8 @@ mod tests { ) } - async fn body_string(body: BoxBody) -> String { - let bytes = hyper::body::to_bytes(body).await.unwrap(); + async fn body_string(body: Body) -> String { + let bytes = body.collect().await.unwrap().to_bytes(); String::from_utf8_lossy(&bytes).into() } diff --git a/src/lib.rs b/src/lib.rs index ef15492..551e8d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,13 +48,15 @@ //! let app = Router::new().route("/", get(handler)) //! .layer(SecureClientIpSource::ConnectInfo.into_extension()); //! -//! axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) -//! .serve( -//! // Don't forget to add `ConnectInfo` if you aren't behind a proxy -//! app.into_make_service_with_connect_info::() -//! ) -//! .await -//! .unwrap() +//! let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); +//! let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); +//! axum::serve( +//! listener, +//! // Don't forget to add `ConnectInfo` if you aren't behind a proxy +//! app.into_make_service_with_connect_info::(), +//! ) +//! .await +//! .unwrap() //! } //! ``` //! diff --git a/src/rudimental.rs b/src/rudimental.rs index 8cf4b3c..024839c 100644 --- a/src/rudimental.rs +++ b/src/rudimental.rs @@ -282,15 +282,16 @@ mod tests { RightmostForwarded, RightmostXForwardedFor, TrueClientIp, XForwardedFor, XRealIp, }; use axum::{ - body::{Body, BoxBody}, + body::Body, http::{Request, StatusCode}, routing::get, Router, }; + use http_body_util::BodyExt; use tower::ServiceExt; - async fn body_string(body: BoxBody) -> String { - let bytes = hyper::body::to_bytes(body).await.unwrap(); + async fn body_string(body: Body) -> String { + let bytes = body.collect().await.unwrap().to_bytes(); String::from_utf8_lossy(&bytes).into() }