-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tower already has `option_layer` for conditionally applying a `Layer` but since it uses `tower::util::Either` it always changes the error type to `BoxError`. That requires using `HandleErrorLayer` which is inconvenient for axum users. That has been changed in tower (tower-rs/tower#637) but still has some issues (tower-rs/tower#665). Its also a breaking change so hasn't been released yet. In the meantime I figure we can provide our own thing in axum-extra, since we already have an `Either` type there.
- Loading branch information
1 parent
607a20d
commit 550802e
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! Additional middleware utilities. | ||
use crate::either::Either; | ||
use tower_layer::Identity; | ||
|
||
/// Convert an `Option<Layer>` into a [`Layer`]. | ||
/// | ||
/// If the layer is a `Some` it'll be applied, otherwise not. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use axum_extra::either::option_layer; | ||
/// use axum::{Router, routing::get}; | ||
/// use std::time::Duration; | ||
/// use tower_http::timeout::TimeoutLayer; | ||
/// | ||
/// # let option_timeout = Some(Duration::new(10, 0)); | ||
/// let timeout_layer = option_timeout.map(TimeoutLayer::new); | ||
/// | ||
/// let app = Router::new() | ||
/// .route("/", get(|| async {})) | ||
/// .layer(option_layer(timeout_layer)); | ||
/// # let _: Router = app; | ||
/// ``` | ||
/// | ||
/// # Difference between this and [`tower::util::option_layer`] | ||
/// | ||
/// [`tower::util::option_layer`] always changes the error type to [`BoxError`] which requires | ||
/// using [`HandleErrorLayer`] when used with axum, even if the layer you're applying uses | ||
/// [`Infallible`]. | ||
/// | ||
/// `axum_extra::middleware::option_layer` on the other hand doesn't change the error type so can | ||
/// be applied directly. | ||
/// | ||
/// [`Layer`]: tower_layer::Layer | ||
/// [`BoxError`]: tower::BoxError | ||
/// [`HandleErrorLayer`]: axum::error_handling::HandleErrorLayer | ||
/// [`Infallible`]: std::convert::Infallible | ||
pub fn option_layer<L>(layer: Option<L>) -> Either<L, Identity> { | ||
layer | ||
.map(Either::E1) | ||
.unwrap_or_else(|| Either::E2(Identity::new())) | ||
} |