Move middleware::from_fn into axum (#719)

* Move `middleware::from_fn` into axum

* changelog

* fix feature

* Rephrase changelog a bit
This commit is contained in:
David Pedersen 2022-01-25 10:19:06 +01:00 committed by GitHub
parent 208720be53
commit 9004a14302
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 35 deletions

View file

@ -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:** `<Cached<T> 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)

View file

@ -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"

View file

@ -44,6 +44,5 @@
#![cfg_attr(test, allow(clippy::float_cmp))]
pub mod extract;
pub mod middleware;
pub mod response;
pub mod routing;

View file

@ -1,5 +0,0 @@
//! Additional types for creating middleware.
pub mod middleware_fn;
pub use self::middleware_fn::{from_fn, Next};

View file

@ -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<str>` instead of `Into<String>`
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)

View file

@ -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"

View file

@ -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;

View file

@ -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<B>(req: Request<B>, next: Next<B>) -> 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: F) -> MiddlewareFnLayer<F> {
MiddlewareFnLayer { f }
pub fn from_fn<F>(f: F) -> FromFnLayer<F> {
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<F> {
pub struct FromFnLayer<F> {
f: F,
}
impl<S, F> Layer<S> for MiddlewareFnLayer<F>
impl<S, F> Layer<S> for FromFnLayer<F>
where
F: Clone,
{
type Service = MiddlewareFn<F, S>;
type Service = FromFn<F, S>;
fn layer(&self, inner: S) -> Self::Service {
MiddlewareFn {
FromFn {
f: self.f.clone(),
inner,
}
}
}
impl<F> fmt::Debug for MiddlewareFnLayer<F> {
impl<F> fmt::Debug for FromFnLayer<F> {
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::<F>()` would
.field("f", &format_args!("{}", type_name::<F>()))
.finish()
@ -179,12 +175,12 @@ impl<F> fmt::Debug for MiddlewareFnLayer<F> {
///
/// Created with [`from_fn`]. See that function for more details.
#[derive(Clone, Copy)]
pub struct MiddlewareFn<F, S> {
pub struct FromFn<F, S> {
f: F,
inner: S,
}
impl<F, Fut, Out, S, ReqBody, ResBody> Service<Request<ReqBody>> for MiddlewareFn<F, S>
impl<F, Fut, Out, S, ReqBody, ResBody> Service<Request<ReqBody>> for FromFn<F, S>
where
F: FnMut(Request<ReqBody>, Next<ReqBody>) -> Fut,
Fut: Future<Output = Out>,
@ -221,12 +217,12 @@ where
}
}
impl<F, S> fmt::Debug for MiddlewareFn<F, S>
impl<F, S> fmt::Debug for FromFn<F, S>
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::<F>()))
.field("inner", &self.inner)
.finish()
@ -250,14 +246,14 @@ impl<ReqBody> Next<ReqBody> {
impl<ReqBody> fmt::Debug for Next<ReqBody> {
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<F> {
#[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;

View file

@ -0,0 +1,5 @@
//! Utilities for writing middleware
mod from_fn;
pub use self::from_fn::{from_fn, FromFn, FromFnLayer, Next};

View file

@ -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]

View file

@ -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,