mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-05 02:22:03 +01:00
Support running extractors from middleware::from_fn
This commit is contained in:
parent
f8c8f5b697
commit
2e80ebd18d
2 changed files with 158 additions and 65 deletions
|
@ -17,11 +17,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
the fallback. Consider using `axum_extra::routing::RouterExt::route_with_tsr`
|
the fallback. Consider using `axum_extra::routing::RouterExt::route_with_tsr`
|
||||||
if you want the old behavior ([#1119])
|
if you want the old behavior ([#1119])
|
||||||
- **added** Implement `TryFrom<http:: Method>` for `MethodFilter` and use new `NoMatchingMethodFilter` error in case of failure ([#1130])
|
- **added** Implement `TryFrom<http:: Method>` for `MethodFilter` and use new `NoMatchingMethodFilter` error in case of failure ([#1130])
|
||||||
|
- **added:** Support running extractors from `middleware::from_fn` functions ([#1088])
|
||||||
|
|
||||||
[#1130]: https://github.com/tokio-rs/axum/pull/1130
|
|
||||||
[#1077]: https://github.com/tokio-rs/axum/pull/1077
|
[#1077]: https://github.com/tokio-rs/axum/pull/1077
|
||||||
|
[#1088]: https://github.com/tokio-rs/axum/pull/1088
|
||||||
[#1102]: https://github.com/tokio-rs/axum/pull/1102
|
[#1102]: https://github.com/tokio-rs/axum/pull/1102
|
||||||
[#1119]: https://github.com/tokio-rs/axum/pull/1119
|
[#1119]: https://github.com/tokio-rs/axum/pull/1119
|
||||||
|
[#1130]: https://github.com/tokio-rs/axum/pull/1130
|
||||||
[#924]: https://github.com/tokio-rs/axum/pull/924
|
[#924]: https://github.com/tokio-rs/axum/pull/924
|
||||||
|
|
||||||
# 0.5.10 (28. June, 2022)
|
# 0.5.10 (28. June, 2022)
|
||||||
|
|
|
@ -3,13 +3,15 @@ use crate::{
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
BoxError,
|
BoxError,
|
||||||
};
|
};
|
||||||
|
use axum_core::extract::{FromRequest, RequestParts};
|
||||||
|
use futures_util::future::BoxFuture;
|
||||||
use http::Request;
|
use http::Request;
|
||||||
use pin_project_lite::pin_project;
|
|
||||||
use std::{
|
use std::{
|
||||||
any::type_name,
|
any::type_name,
|
||||||
convert::Infallible,
|
convert::Infallible,
|
||||||
fmt,
|
fmt,
|
||||||
future::Future,
|
future::Future,
|
||||||
|
marker::PhantomData,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
@ -23,8 +25,8 @@ use tower_service::Service;
|
||||||
/// `from_fn` requires the function given to
|
/// `from_fn` requires the function given to
|
||||||
///
|
///
|
||||||
/// 1. Be an `async fn`.
|
/// 1. Be an `async fn`.
|
||||||
/// 2. Take [`Request<B>`](http::Request) as the first argument.
|
/// 2. Take one or more [extractors] as the first arguments.
|
||||||
/// 3. Take [`Next<B>`](Next) as the second argument.
|
/// 3. Take [`Next<B>`](Next) as the final argument.
|
||||||
/// 4. Return something that implements [`IntoResponse`].
|
/// 4. Return something that implements [`IntoResponse`].
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
@ -62,6 +64,37 @@ use tower_service::Service;
|
||||||
/// # let app: Router = app;
|
/// # let app: Router = app;
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// # Running extractors
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use axum::{
|
||||||
|
/// Router,
|
||||||
|
/// extract::{TypedHeader, Query},
|
||||||
|
/// headers::authorization::{Authorization, Bearer},
|
||||||
|
/// http::Request,
|
||||||
|
/// middleware::{self, Next},
|
||||||
|
/// response::Response,
|
||||||
|
/// routing::get,
|
||||||
|
/// };
|
||||||
|
/// use std::collections::HashMap;
|
||||||
|
///
|
||||||
|
/// async fn my_middleware<B>(
|
||||||
|
/// TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
|
||||||
|
/// Query(query_params): Query<HashMap<String, String>>,
|
||||||
|
/// req: Request<B>,
|
||||||
|
/// next: Next<B>,
|
||||||
|
/// ) -> Response {
|
||||||
|
/// // do something with `auth` and `query_params`...
|
||||||
|
///
|
||||||
|
/// next.run(req).await
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let app = Router::new()
|
||||||
|
/// .route("/", get(|| async { /* ... */ }))
|
||||||
|
/// .route_layer(middleware::from_fn(my_middleware));
|
||||||
|
/// # let app: Router = app;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// # Passing state
|
/// # Passing state
|
||||||
///
|
///
|
||||||
/// State can be passed to the function like so:
|
/// State can be passed to the function like so:
|
||||||
|
@ -114,11 +147,10 @@ use tower_service::Service;
|
||||||
/// struct State { /* ... */ }
|
/// struct State { /* ... */ }
|
||||||
///
|
///
|
||||||
/// async fn my_middleware<B>(
|
/// async fn my_middleware<B>(
|
||||||
|
/// Extension(state): Extension<State>,
|
||||||
/// req: Request<B>,
|
/// req: Request<B>,
|
||||||
/// next: Next<B>,
|
/// next: Next<B>,
|
||||||
/// ) -> Response {
|
/// ) -> Response {
|
||||||
/// let state: &State = req.extensions().get().unwrap();
|
|
||||||
///
|
|
||||||
/// // ...
|
/// // ...
|
||||||
/// # ().into_response()
|
/// # ().into_response()
|
||||||
/// }
|
/// }
|
||||||
|
@ -134,8 +166,13 @@ use tower_service::Service;
|
||||||
/// );
|
/// );
|
||||||
/// # let app: Router = app;
|
/// # let app: Router = app;
|
||||||
/// ```
|
/// ```
|
||||||
pub fn from_fn<F>(f: F) -> FromFnLayer<F> {
|
///
|
||||||
FromFnLayer { f }
|
/// [extractors]: crate::extract::FromRequest
|
||||||
|
pub fn from_fn<F, T>(f: F) -> FromFnLayer<F, T> {
|
||||||
|
FromFnLayer {
|
||||||
|
f,
|
||||||
|
_extractor: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A [`tower::Layer`] from an async function.
|
/// A [`tower::Layer`] from an async function.
|
||||||
|
@ -143,26 +180,41 @@ pub fn from_fn<F>(f: F) -> FromFnLayer<F> {
|
||||||
/// [`tower::Layer`] is used to apply middleware to [`Router`](crate::Router)'s.
|
/// [`tower::Layer`] is used to apply middleware to [`Router`](crate::Router)'s.
|
||||||
///
|
///
|
||||||
/// Created with [`from_fn`]. See that function for more details.
|
/// Created with [`from_fn`]. See that function for more details.
|
||||||
#[derive(Clone, Copy)]
|
pub struct FromFnLayer<F, T> {
|
||||||
pub struct FromFnLayer<F> {
|
|
||||||
f: F,
|
f: F,
|
||||||
|
_extractor: PhantomData<fn() -> T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, F> Layer<S> for FromFnLayer<F>
|
impl<F, T> Clone for FromFnLayer<F, T>
|
||||||
where
|
where
|
||||||
F: Clone,
|
F: Clone,
|
||||||
{
|
{
|
||||||
type Service = FromFn<F, S>;
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
f: self.f.clone(),
|
||||||
|
_extractor: self._extractor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, T> Copy for FromFnLayer<F, T> where F: Copy {}
|
||||||
|
|
||||||
|
impl<S, F, T> Layer<S> for FromFnLayer<F, T>
|
||||||
|
where
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
type Service = FromFn<F, S, T>;
|
||||||
|
|
||||||
fn layer(&self, inner: S) -> Self::Service {
|
fn layer(&self, inner: S) -> Self::Service {
|
||||||
FromFn {
|
FromFn {
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
inner,
|
inner,
|
||||||
|
_extractor: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> fmt::Debug for FromFnLayer<F> {
|
impl<F, T> fmt::Debug for FromFnLayer<F, T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("FromFnLayer")
|
f.debug_struct("FromFnLayer")
|
||||||
// Write out the type name, without quoting it as `&type_name::<F>()` would
|
// Write out the type name, without quoting it as `&type_name::<F>()` would
|
||||||
|
@ -174,50 +226,94 @@ impl<F> fmt::Debug for FromFnLayer<F> {
|
||||||
/// A middleware created from an async function.
|
/// A middleware created from an async function.
|
||||||
///
|
///
|
||||||
/// Created with [`from_fn`]. See that function for more details.
|
/// Created with [`from_fn`]. See that function for more details.
|
||||||
#[derive(Clone, Copy)]
|
pub struct FromFn<F, S, T> {
|
||||||
pub struct FromFn<F, S> {
|
|
||||||
f: F,
|
f: F,
|
||||||
inner: S,
|
inner: S,
|
||||||
|
_extractor: PhantomData<fn() -> T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Fut, Out, S, ReqBody, ResBody> Service<Request<ReqBody>> for FromFn<F, S>
|
impl<F, S, T> Clone for FromFn<F, S, T>
|
||||||
where
|
where
|
||||||
F: FnMut(Request<ReqBody>, Next<ReqBody>) -> Fut,
|
F: Clone,
|
||||||
Fut: Future<Output = Out>,
|
S: Clone,
|
||||||
Out: IntoResponse,
|
|
||||||
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = Infallible>
|
|
||||||
+ Clone
|
|
||||||
+ Send
|
|
||||||
+ 'static,
|
|
||||||
S::Future: Send + 'static,
|
|
||||||
ResBody: HttpBody<Data = Bytes> + Send + 'static,
|
|
||||||
ResBody::Error: Into<BoxError>,
|
|
||||||
{
|
{
|
||||||
type Response = Response;
|
fn clone(&self) -> Self {
|
||||||
type Error = Infallible;
|
Self {
|
||||||
type Future = ResponseFuture<Fut>;
|
f: self.f.clone(),
|
||||||
|
inner: self.inner.clone(),
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
_extractor: self._extractor,
|
||||||
self.inner.poll_ready(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
|
|
||||||
let not_ready_inner = self.inner.clone();
|
|
||||||
let ready_inner = std::mem::replace(&mut self.inner, not_ready_inner);
|
|
||||||
|
|
||||||
let inner = ServiceBuilder::new()
|
|
||||||
.boxed_clone()
|
|
||||||
.map_response_body(body::boxed)
|
|
||||||
.service(ready_inner);
|
|
||||||
let next = Next { inner };
|
|
||||||
|
|
||||||
ResponseFuture {
|
|
||||||
inner: (self.f)(req, next),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, S> fmt::Debug for FromFn<F, S>
|
impl<F, S, T> Copy for FromFn<F, S, T>
|
||||||
|
where
|
||||||
|
F: Copy,
|
||||||
|
S: Copy,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_service {
|
||||||
|
( $($ty:ident),* $(,)? ) => {
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
impl<F, Fut, Out, S, ReqBody, ResBody, $($ty,)*> Service<Request<ReqBody>> for FromFn<F, S, ($($ty,)*)>
|
||||||
|
where
|
||||||
|
F: FnMut($($ty),*, Next<ReqBody>) -> Fut + Clone + Send + 'static,
|
||||||
|
$( $ty: FromRequest<ReqBody> + Send, )*
|
||||||
|
Fut: Future<Output = Out> + Send + 'static,
|
||||||
|
Out: IntoResponse + 'static,
|
||||||
|
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = Infallible>
|
||||||
|
+ Clone
|
||||||
|
+ Send
|
||||||
|
+ 'static,
|
||||||
|
S::Future: Send + 'static,
|
||||||
|
ReqBody: Send + 'static,
|
||||||
|
ResBody: HttpBody<Data = Bytes> + Send + 'static,
|
||||||
|
ResBody::Error: Into<BoxError>,
|
||||||
|
{
|
||||||
|
type Response = Response;
|
||||||
|
type Error = Infallible;
|
||||||
|
type Future = ResponseFuture;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.inner.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
|
||||||
|
let not_ready_inner = self.inner.clone();
|
||||||
|
let ready_inner = std::mem::replace(&mut self.inner, not_ready_inner);
|
||||||
|
|
||||||
|
let mut f = self.f.clone();
|
||||||
|
|
||||||
|
let future = Box::pin(async move {
|
||||||
|
let mut parts = RequestParts::new(req);
|
||||||
|
$(
|
||||||
|
let $ty = match $ty::from_request(&mut parts).await {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(rejection) => return rejection.into_response(),
|
||||||
|
};
|
||||||
|
)*
|
||||||
|
|
||||||
|
let inner = ServiceBuilder::new()
|
||||||
|
.boxed_clone()
|
||||||
|
.map_response_body(body::boxed)
|
||||||
|
.service(ready_inner);
|
||||||
|
let next = Next { inner };
|
||||||
|
|
||||||
|
f($($ty),*, next).await.into_response()
|
||||||
|
});
|
||||||
|
|
||||||
|
ResponseFuture {
|
||||||
|
inner: future
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
all_the_tuples!(impl_service);
|
||||||
|
|
||||||
|
impl<F, S, T> fmt::Debug for FromFn<F, S, T>
|
||||||
where
|
where
|
||||||
S: fmt::Debug,
|
S: fmt::Debug,
|
||||||
{
|
{
|
||||||
|
@ -252,27 +348,22 @@ impl<ReqBody> fmt::Debug for Next<ReqBody> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pin_project! {
|
/// Response future for [`FromFn`].
|
||||||
/// Response future for [`FromFn`].
|
pub struct ResponseFuture {
|
||||||
pub struct ResponseFuture<F> {
|
inner: BoxFuture<'static, Response>,
|
||||||
#[pin]
|
}
|
||||||
inner: F,
|
|
||||||
|
impl Future for ResponseFuture {
|
||||||
|
type Output = Result<Response, Infallible>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
self.inner.as_mut().poll(cx).map(Ok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Out> Future for ResponseFuture<F>
|
impl fmt::Debug for ResponseFuture {
|
||||||
where
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
F: Future<Output = Out>,
|
f.debug_struct("ResponseFuture").finish()
|
||||||
Out: IntoResponse,
|
|
||||||
{
|
|
||||||
type Output = Result<Response, Infallible>;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
self.project()
|
|
||||||
.inner
|
|
||||||
.poll(cx)
|
|
||||||
.map(IntoResponse::into_response)
|
|
||||||
.map(Ok)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue