From 9004a143021727e9ac91193d14fdfefc87f04397 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 25 Jan 2022 10:19:06 +0100 Subject: [PATCH] Move `middleware::from_fn` into axum (#719) * Move `middleware::from_fn` into axum * changelog * fix feature * Rephrase changelog a bit --- axum-extra/CHANGELOG.md | 2 + axum-extra/Cargo.toml | 2 +- axum-extra/src/lib.rs | 1 - axum-extra/src/middleware/mod.rs | 5 --- axum/CHANGELOG.md | 3 ++ axum/Cargo.toml | 2 +- axum/src/lib.rs | 1 + .../src/middleware/from_fn.rs | 44 +++++++++---------- axum/src/middleware/mod.rs | 5 +++ examples/print-request-response/src/main.rs | 2 +- examples/prometheus-metrics/src/main.rs | 10 ++++- 11 files changed, 42 insertions(+), 35 deletions(-) delete mode 100644 axum-extra/src/middleware/mod.rs rename axum-extra/src/middleware/middleware_fn.rs => axum/src/middleware/from_fn.rs (89%) create mode 100644 axum/src/middleware/mod.rs diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index beaf06a2..5a327d5f 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **breaking:** `CachedRejection` has been removed ([#699]) - **breaking:** ` as FromRequest>::Rejection` is now `T::Rejection`. ([#699]) +- **breaking:** `middleware::from_fn` has been moved into the main axum crate ([#719]) [#666]: https://github.com/tokio-rs/axum/pull/666 [#699]: https://github.com/tokio-rs/axum/pull/699 +[#719]: https://github.com/tokio-rs/axum/pull/719 # 0.1.2 (13. January, 2021) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index e0be1711..7da51d18 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -20,7 +20,7 @@ http = "0.2" mime = "0.3" pin-project-lite = "0.2" tower = { version = "0.4", default_features = false, features = ["util"] } -tower-http = { version = "0.2", features = ["util", "map-response-body"] } +tower-http = { version = "0.2", features = ["map-response-body"] } tower-layer = "0.3" tower-service = "0.3" diff --git a/axum-extra/src/lib.rs b/axum-extra/src/lib.rs index daf0a8d5..bb16461f 100644 --- a/axum-extra/src/lib.rs +++ b/axum-extra/src/lib.rs @@ -44,6 +44,5 @@ #![cfg_attr(test, allow(clippy::float_cmp))] pub mod extract; -pub mod middleware; pub mod response; pub mod routing; diff --git a/axum-extra/src/middleware/mod.rs b/axum-extra/src/middleware/mod.rs deleted file mode 100644 index 89420678..00000000 --- a/axum-extra/src/middleware/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -//! Additional types for creating middleware. - -pub mod middleware_fn; - -pub use self::middleware_fn::{from_fn, Next}; diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 26ffa5cb..151a784c 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **added:** `middleware::from_fn` for creating middleware from async functions. + This previously lived in axum-extra but has been moved to axum ([#719]) - **breaking:** `sse::Event` now accepts types implementing `AsRef` instead of `Into` as field values. - **breaking:** `sse::Event` now panics if a setter method is called twice instead of silently @@ -54,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#665]: https://github.com/tokio-rs/axum/pull/665 [#698]: https://github.com/tokio-rs/axum/pull/698 [#699]: https://github.com/tokio-rs/axum/pull/699 +[#719]: https://github.com/tokio-rs/axum/pull/719 # 0.4.4 (13. January, 2021) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 92cbbf0e..ceffd04f 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -42,7 +42,7 @@ sync_wrapper = "0.1.1" tokio = { version = "1", features = ["time"] } tokio-util = "0.6" tower = { version = "0.4.11", default-features = false, features = ["util", "buffer", "make"] } -tower-http = { version = "0.2.0", features = ["map-response-body"] } +tower-http = { version = "0.2.0", features = ["util", "map-response-body"] } tower-layer = "0.3" tower-service = "0.3" diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 29ea6a17..238dd22a 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -466,6 +466,7 @@ pub mod body; pub mod error_handling; pub mod extract; pub mod handler; +pub mod middleware; pub mod response; pub mod routing; diff --git a/axum-extra/src/middleware/middleware_fn.rs b/axum/src/middleware/from_fn.rs similarity index 89% rename from axum-extra/src/middleware/middleware_fn.rs rename to axum/src/middleware/from_fn.rs index 0a0a9fe4..8e68073f 100644 --- a/axum-extra/src/middleware/middleware_fn.rs +++ b/axum/src/middleware/from_fn.rs @@ -1,8 +1,4 @@ -//! Create middleware from async functions. -//! -//! See [`from_fn`] for more details. - -use axum::{ +use crate::{ body::{self, Bytes, HttpBody}, response::{IntoResponse, Response}, BoxError, @@ -39,8 +35,8 @@ use tower_service::Service; /// http::{Request, StatusCode}, /// routing::get, /// response::IntoResponse, +/// middleware::{self, Next}, /// }; -/// use axum_extra::middleware::{self, Next}; /// /// async fn auth(req: Request, next: Next) -> impl IntoResponse { /// let auth_header = req.headers() @@ -76,8 +72,8 @@ use tower_service::Service; /// http::{Request, StatusCode}, /// routing::get, /// response::IntoResponse, +/// middleware::{self, Next} /// }; -/// use axum_extra::middleware::{self, Next}; /// /// #[derive(Clone)] /// struct State { /* ... */ } @@ -109,9 +105,9 @@ use tower_service::Service; /// http::{Request, StatusCode}, /// routing::get, /// response::IntoResponse, +/// middleware::{self, Next}, /// AddExtensionLayer, /// }; -/// use axum_extra::middleware::{self, Next}; /// use tower::ServiceBuilder; /// /// #[derive(Clone)] @@ -138,37 +134,37 @@ use tower_service::Service; /// ); /// # let app: Router = app; /// ``` -pub fn from_fn(f: F) -> MiddlewareFnLayer { - MiddlewareFnLayer { f } +pub fn from_fn(f: F) -> FromFnLayer { + FromFnLayer { f } } /// A [`tower::Layer`] from an async function. /// -/// [`tower::Layer`] is used to apply middleware to [`axum::Router`]s. +/// [`tower::Layer`] is used to apply middleware to [`Router`](crate::Router)'s. /// /// Created with [`from_fn`]. See that function for more details. #[derive(Clone, Copy)] -pub struct MiddlewareFnLayer { +pub struct FromFnLayer { f: F, } -impl Layer for MiddlewareFnLayer +impl Layer for FromFnLayer where F: Clone, { - type Service = MiddlewareFn; + type Service = FromFn; fn layer(&self, inner: S) -> Self::Service { - MiddlewareFn { + FromFn { f: self.f.clone(), inner, } } } -impl fmt::Debug for MiddlewareFnLayer { +impl fmt::Debug for FromFnLayer { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("MiddlewareFnLayer") + f.debug_struct("FromFnLayer") // Write out the type name, without quoting it as `&type_name::()` would .field("f", &format_args!("{}", type_name::())) .finish() @@ -179,12 +175,12 @@ impl fmt::Debug for MiddlewareFnLayer { /// /// Created with [`from_fn`]. See that function for more details. #[derive(Clone, Copy)] -pub struct MiddlewareFn { +pub struct FromFn { f: F, inner: S, } -impl Service> for MiddlewareFn +impl Service> for FromFn where F: FnMut(Request, Next) -> Fut, Fut: Future, @@ -221,12 +217,12 @@ where } } -impl fmt::Debug for MiddlewareFn +impl fmt::Debug for FromFn where S: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("MiddlewareFnLayer") + f.debug_struct("FromFnLayer") .field("f", &format_args!("{}", type_name::())) .field("inner", &self.inner) .finish() @@ -250,14 +246,14 @@ impl Next { impl fmt::Debug for Next { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("MiddlewareFnLayer") + f.debug_struct("FromFnLayer") .field("inner", &self.inner) .finish() } } pin_project! { - /// Response future for [`MiddlewareFn`]. + /// Response future for [`FromFn`]. pub struct ResponseFuture { #[pin] inner: F, @@ -283,7 +279,7 @@ where #[cfg(test)] mod tests { use super::*; - use axum::{body::Empty, routing::get, Router}; + use crate::{body::Empty, routing::get, Router}; use http::{HeaderMap, StatusCode}; use tower::ServiceExt; diff --git a/axum/src/middleware/mod.rs b/axum/src/middleware/mod.rs new file mode 100644 index 00000000..cd0cd349 --- /dev/null +++ b/axum/src/middleware/mod.rs @@ -0,0 +1,5 @@ +//! Utilities for writing middleware + +mod from_fn; + +pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next}; diff --git a/examples/print-request-response/src/main.rs b/examples/print-request-response/src/main.rs index 97ce4355..75109fb3 100644 --- a/examples/print-request-response/src/main.rs +++ b/examples/print-request-response/src/main.rs @@ -7,11 +7,11 @@ use axum::{ body::{Body, Bytes}, http::{Request, StatusCode}, + middleware::{self, Next}, response::{IntoResponse, Response}, routing::post, Router, }; -use axum_extra::middleware::{self, Next}; use std::net::SocketAddr; #[tokio::main] diff --git a/examples/prometheus-metrics/src/main.rs b/examples/prometheus-metrics/src/main.rs index d8e8ec21..a27747e9 100644 --- a/examples/prometheus-metrics/src/main.rs +++ b/examples/prometheus-metrics/src/main.rs @@ -7,8 +7,14 @@ //! cargo run -p example-prometheus-metrics //! ``` -use axum::{extract::MatchedPath, http::Request, response::IntoResponse, routing::get, Router}; -use axum_extra::middleware::{self, Next}; +use axum::{ + extract::MatchedPath, + http::Request, + middleware::{self, Next}, + response::IntoResponse, + routing::get, + Router, +}; use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle}; use std::{ future::ready,