From 76d931317161bda8b32671ea25ca41378043d4f6 Mon Sep 17 00:00:00 2001 From: Heki <75025611+LinuxHeki@users.noreply.github.com> Date: Fri, 3 Nov 2023 20:16:31 +0000 Subject: [PATCH] Allow ActixSettings to be applied to HttpServer in actix-settings (#321) * allow other settings objects to be applied * update changelog * update changelog --------- Co-authored-by: LinuxHeki Co-authored-by: Rob Ede --- actix-settings/CHANGES.md | 2 ++ actix-settings/src/lib.rs | 54 +++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/actix-settings/CHANGES.md b/actix-settings/CHANGES.md index 8efdeae48..dbb4d3f25 100644 --- a/actix-settings/CHANGES.md +++ b/actix-settings/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- The `ApplySettings` trait now includes a type parameter, allowing multiple types to be implemented per configuration target. +- Implement `ApplySettings` for `ActixSettings`. - Rename `AtError => Error`. - Remove `AtResult` type alias. - Update `toml` dependency to `0.8`. diff --git a/actix-settings/src/lib.rs b/actix-settings/src/lib.rs index 2f26ab164..ba558331f 100644 --- a/actix-settings/src/lib.rs +++ b/actix-settings/src/lib.rs @@ -243,17 +243,13 @@ where } /// Extension trait for applying parsed settings to the server object. -pub trait ApplySettings { - /// Apply a [`BasicSettings`] value to `self`. - /// - /// [`BasicSettings`]: ./struct.BasicSettings.html +pub trait ApplySettings { + /// Apply some settings object value to `self`. #[must_use] - fn apply_settings(self, settings: &BasicSettings) -> Self - where - A: de::DeserializeOwned; + fn apply_settings(self, settings: &S) -> Self; } -impl ApplySettings for HttpServer +impl ApplySettings for HttpServer where F: Fn() -> I + Send + Clone + 'static, I: IntoServiceFactory, @@ -264,51 +260,48 @@ where S::Future: 'static, B: MessageBody + 'static, { - fn apply_settings(mut self, settings: &BasicSettings) -> Self - where - A: de::DeserializeOwned, - { - if settings.actix.tls.enabled { + fn apply_settings(mut self, settings: &ActixSettings) -> Self { + if settings.tls.enabled { // for Address { host, port } in &settings.actix.hosts { // self = self.bind(format!("{}:{}", host, port)) // .unwrap(/*TODO*/); // } todo!("[ApplySettings] TLS support has not been implemented yet."); } else { - for Address { host, port } in &settings.actix.hosts { + for Address { host, port } in &settings.hosts { self = self.bind(format!("{host}:{port}")) .unwrap(/*TODO*/); } } - self = match settings.actix.num_workers { + self = match settings.num_workers { NumWorkers::Default => self, NumWorkers::Manual(n) => self.workers(n), }; - self = match settings.actix.backlog { + self = match settings.backlog { Backlog::Default => self, Backlog::Manual(n) => self.backlog(n as u32), }; - self = match settings.actix.max_connections { + self = match settings.max_connections { MaxConnections::Default => self, MaxConnections::Manual(n) => self.max_connections(n), }; - self = match settings.actix.max_connection_rate { + self = match settings.max_connection_rate { MaxConnectionRate::Default => self, MaxConnectionRate::Manual(n) => self.max_connection_rate(n), }; - self = match settings.actix.keep_alive { + self = match settings.keep_alive { KeepAlive::Default => self, KeepAlive::Disabled => self.keep_alive(ActixKeepAlive::Disabled), KeepAlive::Os => self.keep_alive(ActixKeepAlive::Os), KeepAlive::Seconds(n) => self.keep_alive(Duration::from_secs(n as u64)), }; - self = match settings.actix.client_timeout { + self = match settings.client_timeout { Timeout::Default => self, Timeout::Milliseconds(n) => { self.client_request_timeout(Duration::from_millis(n as u64)) @@ -316,7 +309,7 @@ where Timeout::Seconds(n) => self.client_request_timeout(Duration::from_secs(n as u64)), }; - self = match settings.actix.client_shutdown { + self = match settings.client_shutdown { Timeout::Default => self, Timeout::Milliseconds(n) => { self.client_disconnect_timeout(Duration::from_millis(n as u64)) @@ -324,7 +317,7 @@ where Timeout::Seconds(n) => self.client_disconnect_timeout(Duration::from_secs(n as u64)), }; - self = match settings.actix.shutdown_timeout { + self = match settings.shutdown_timeout { Timeout::Default => self, Timeout::Milliseconds(_) => self.shutdown_timeout(1), Timeout::Seconds(n) => self.shutdown_timeout(n as u64), @@ -334,6 +327,23 @@ where } } +impl ApplySettings> for HttpServer +where + F: Fn() -> I + Send + Clone + 'static, + I: IntoServiceFactory, + S: ServiceFactory + 'static, + S::Error: Into + 'static, + S::InitError: fmt::Debug, + S::Response: Into> + 'static, + S::Future: 'static, + B: MessageBody + 'static, + A: de::DeserializeOwned, +{ + fn apply_settings(self, settings: &BasicSettings) -> Self { + self.apply_settings(&settings.actix) + } +} + #[cfg(test)] mod tests { use actix_web::App;