From f0637556a81e779307989950d63e40dd18775e39 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Wed, 14 Aug 2024 07:01:26 +0800 Subject: [PATCH] chore(deps): update tower to 0.5 and move tower_compat to salvo_extra (#866) * chore(deps): update tower requirement from 0.4 to 0.5 * Move tower compat to salvo_extra * fix doc * fix ci --- Cargo.toml | 2 +- crates/core/Cargo.toml | 5 +---- crates/core/src/lib.rs | 10 ---------- crates/extra/Cargo.toml | 8 ++++++-- crates/extra/src/lib.rs | 6 ++++++ crates/{core => extra}/src/tower_compat.rs | 13 +++++++------ crates/salvo/Cargo.toml | 2 +- crates/salvo/src/lib.rs | 4 ++++ 8 files changed, 26 insertions(+), 24 deletions(-) rename crates/{core => extra}/src/tower_compat.rs (97%) diff --git a/Cargo.toml b/Cargo.toml index c5e2b98db..717394b47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,7 +124,7 @@ tokio-openssl = "0.6" tokio-stream = { version = "0.1", default-features = false } tokio-tungstenite = { version = "0.23", default-features = false } tokio-util = "0.7" -tower = { version = "0.4", default-features = false } +tower = { version = "0.5", default-features = false } tracing-subscriber = { version = "0.3" } tracing = "0.1" tracing-test = "0.2.1" diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 19c870056..aa69a49ab 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["cookie", "fix-http1-request-uri", "server", "server-handle", "http1", "http2", "test", "ring"] -full = ["cookie", "fix-http1-request-uri", "server", "http1", "http2", "http2-cleartext", "quinn", "rustls", "native-tls", "openssl", "unix", "test", "tower-compat", "anyhow", "eyre", "ring", "socket2"] +full = ["cookie", "fix-http1-request-uri", "server", "http1", "http2", "http2-cleartext", "quinn", "rustls", "native-tls", "openssl", "unix", "test", "anyhow", "eyre", "ring", "socket2"] cookie = ["dep:cookie"] fix-http1-request-uri = ["http1"] server = [] @@ -33,7 +33,6 @@ unix = ["http1"] test = ["dep:brotli", "dep:flate2", "dep:zstd", "dep:encoding_rs", "dep:serde_urlencoded", "dep:url", "tokio/macros"] acme = ["http1", "http2", "hyper-util/http1", "hyper-util/http2", "hyper-util/client-legacy", "dep:hyper-rustls", "dep:rcgen", "dep:ring", "ring", "dep:x509-parser", "dep:tokio-rustls", "dep:rustls-pemfile"] socket2 = ["dep:socket2"] -tower-compat = ["dep:tower"] # aws-lc-rs = ["hyper-rustls?/aws-lc-rs", "tokio-rustls?/aws-lc-rs"] ring = ["hyper-rustls?/ring", "tokio-rustls?/ring"] @@ -86,7 +85,6 @@ tokio-native-tls = { workspace = true, optional = true } tokio-openssl = { workspace = true, optional = true } tokio-rustls = { workspace = true, optional = true, features = ["logging", "tls12"]} tokio-util = { workspace = true, features = ["io"] } -tower = { workspace = true, optional = true, default-features = false, features = ["buffer", "util"] } tracing = { workspace = true } url = { workspace = true, optional = true } x509-parser = { workspace = true, optional = true } @@ -100,7 +98,6 @@ nix = { workspace = true, features = ["fs", "user"] } [dev-dependencies] fastrand = { workspace = true } -tower = { workspace = true, features = ["limit"]} [lints] workspace = true diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index e9ef0237a..bfce58b0f 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -19,7 +19,6 @@ //! | `openssl` | TLS built on [`openssl-tls`](https://crates.io/crates/openssl) | ❌ | //! | `native-tls` | TLS built on [`native-tls`](https://crates.io/crates/native-tls) | ❌ | //! | `unix` | Listener based on unix socket | ❌ | -//! | `tower-compat` | Adapters for `tower::Layer` and `tower::Service` | ❌ | //! | `anyhow` | Integrate with the [`anyhow`](https://crates.io/crates/anyhow) crate | ❌ | //! | `eyre` | Integrate with the [`eyre`](https://crates.io/crates/eyre) crate | ❌ | #![doc(html_favicon_url = "https://salvo.rs/favicon-32x32.png")] @@ -67,11 +66,6 @@ cfg_feature! { #![feature ="quinn"] pub use proto::webtransport; } -cfg_feature! { - #![feature ="tower-compat"] - pub mod tower_compat; - pub use tower_compat::{TowerServiceCompat, TowerLayerCompat}; -} pub use self::conn::Listener; pub use self::depot::Depot; @@ -116,10 +110,6 @@ pub mod prelude { #![unix] pub use crate::conn::UnixListener; } - cfg_feature! { - #![feature ="tower-compat"] - pub use crate::tower_compat::{TowerServiceCompat, TowerLayerCompat}; - } pub use crate::conn::{JoinedListener, Listener, TcpListener}; pub use crate::handler::{self, Handler}; pub use crate::routing::{FlowCtrl, Router}; diff --git a/crates/extra/Cargo.toml b/crates/extra/Cargo.toml index e0138bd71..79ff0ef21 100644 --- a/crates/extra/Cargo.toml +++ b/crates/extra/Cargo.toml @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["full"] -full = ["affix", "basic-auth", "caching-headers", "catch-panic", "force-https", "logging", "sse", "concurrency-limiter", "size-limiter", "trailing-slash", "timeout", "websocket", "request-id"] +full = ["affix", "basic-auth", "caching-headers", "catch-panic", "force-https", "logging", "sse", "concurrency-limiter", "size-limiter", "trailing-slash", "timeout", "websocket", "request-id", "tower-compat"] affix = [] basic-auth = ["dep:base64"] caching-headers = ["dep:etag", "dep:tracing"] @@ -31,11 +31,13 @@ trailing-slash = ["dep:tracing"] timeout = ["tokio/macros"] websocket = ["dep:futures-util", "dep:hyper", "tokio", "tokio-tungstenite", "dep:tracing"] request-id = ["dep:ulid"] +tower-compat = ["dep:futures-util", "dep:http-body-util", "dep:tower", "dep:tracing"] [dependencies] base64 = { workspace = true, optional = true } etag = { workspace = true, features = ["std"], optional = true } futures-util = { workspace = true, optional = true } +http-body-util = { workspace = true, optional = true } hyper = { workspace = true, features = ["server", "http1", "http2", "client"], optional = true } pin-project = { workspace = true, optional = true } salvo_core = { workspace = true } @@ -44,13 +46,15 @@ serde_json = { workspace = true, optional = true } tokio = { workspace = true, optional = true } tokio-tungstenite = { workspace = true, optional = true } tokio-util = { workspace = true, features = ["io"], optional = true } +tower = { workspace = true, optional = true, default-features = false, features = ["buffer", "util"] } tracing = { workspace = true, optional = true } ulid = { workspace = true, optional = true, features = ["std"] } [dev-dependencies] -salvo_core = { workspace = true, features = ["http1", "test"] } +salvo_core = { workspace = true, features = ["http1", "server", "test"] } time = { workspace = true } tokio-stream = { workspace = true } +tower = { workspace = true, features = ["limit"]} tracing-test = { workspace = true } http-body-util = { workspace = true } diff --git a/crates/extra/src/lib.rs b/crates/extra/src/lib.rs index e518b200a..30e2da3b1 100644 --- a/crates/extra/src/lib.rs +++ b/crates/extra/src/lib.rs @@ -21,6 +21,7 @@ //! | [`sse`] | Server-Sent Events (SSE) middleware | //! | [`timeout`] | Middleware for setting a timeout | //! | [`trailing-slash`](trailing_slash) | Middleware for handling trailing slashes | +//! | [`tower-compat`](tower_compat) | Adapters for `tower::Layer` and `tower::Service` | //! | [`websocket`] | WebSocket implementation | #![doc(html_favicon_url = "https://salvo.rs/favicon-32x32.png")] #![doc(html_logo_url = "https://salvo.rs/images/logo.svg")] @@ -84,3 +85,8 @@ cfg_feature! { #![feature = "request-id"] pub mod request_id; } +cfg_feature! { + #![feature ="tower-compat"] + pub mod tower_compat; + pub use tower_compat::{TowerServiceCompat, TowerLayerCompat}; +} diff --git a/crates/core/src/tower_compat.rs b/crates/extra/src/tower_compat.rs similarity index 97% rename from crates/core/src/tower_compat.rs rename to crates/extra/src/tower_compat.rs index c91c77984..4eb28789a 100644 --- a/crates/core/src/tower_compat.rs +++ b/crates/extra/src/tower_compat.rs @@ -1,10 +1,11 @@ //! Adapters for [`tower::Layer`](https://docs.rs/tower/latest/tower/trait.Layer.html) and //! [`tower::Service`](https://docs.rs/tower/latest/tower/trait.Service.html). -//! +//! //! # Example //! //! ```no_run //! use salvo_core::prelude::*; +//! use salvo_extra::tower_compat::*; //! use tokio::time::Duration; //! use tower::limit::RateLimitLayer; //! @@ -35,8 +36,8 @@ use hyper::body::{Body, Bytes}; use tower::buffer::Buffer; use tower::{Layer, Service, ServiceExt}; -use crate::http::{ReqBody, ResBody, StatusError}; -use crate::{async_trait, Depot, FlowCtrl, Handler, Request, Response}; +use salvo_core::http::{ReqBody, ResBody, StatusError}; +use salvo_core::{async_trait, hyper, Depot, FlowCtrl, Handler, Request, Response}; /// Trait for tower service compat. pub trait TowerServiceCompat { @@ -191,7 +192,7 @@ pub trait TowerLayerCompat { impl TowerLayerCompat for T where T: Layer + Send + Sync + Sized + 'static {} /// Tower service compat handler. -pub struct TowerLayerHandler>, QB>(Buffer>); +pub struct TowerLayerHandler>, QB>(Buffer, Svc::Future>); #[async_trait] impl Handler for TowerLayerHandler @@ -262,8 +263,8 @@ where mod tests { use super::*; - use crate::test::{ResponseExt, TestClient}; - use crate::{handler, Router}; + use salvo_core::test::{ResponseExt, TestClient}; + use salvo_core::{handler, Router}; #[tokio::test] async fn test_tower_layer() { diff --git a/crates/salvo/Cargo.toml b/crates/salvo/Cargo.toml index 08aa64031..758f59f6d 100644 --- a/crates/salvo/Cargo.toml +++ b/crates/salvo/Cargo.toml @@ -40,7 +40,6 @@ openssl = ["salvo_core/openssl"] unix = ["salvo_core/unix"] acme = ["salvo_core/acme"] socket2 = ["salvo_core/socket2"] -tower-compat = ["salvo_core/tower-compat"] anyhow = ["salvo_core/anyhow"] eyre = ["salvo_core/eyre"] test = ["salvo_core/test"] @@ -60,6 +59,7 @@ timeout = ["salvo_extra/timeout"] websocket = ["salvo_extra/websocket"] request-id = ["salvo_extra/request-id"] caching-headers = ["salvo_extra/caching-headers"] +tower-compat = ["salvo_extra/tower-compat"] cache = ["dep:salvo-cache"] cors = ["dep:salvo-cors"] csrf = ["dep:salvo-csrf"] diff --git a/crates/salvo/src/lib.rs b/crates/salvo/src/lib.rs index 26b79f943..7da236ed2 100644 --- a/crates/salvo/src/lib.rs +++ b/crates/salvo/src/lib.rs @@ -240,6 +240,10 @@ pub mod prelude { #![feature ="timeout"] pub use salvo_extra::timeout::Timeout; } + cfg_feature! { + #![feature ="tower-compat"] + pub use salvo_extra::tower_compat::{TowerServiceCompat, TowerLayerCompat}; + } cfg_feature! { #![feature ="websocket"] pub use salvo_extra::websocket::WebSocketUpgrade;