1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 10:32:55 +01:00

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 <linuxheki@gmail.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Heki 2023-11-03 20:16:31 +00:00 committed by GitHub
parent 373a89a978
commit 76d9313171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 22 deletions

View File

@ -2,6 +2,8 @@
## Unreleased ## 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`. - Rename `AtError => Error`.
- Remove `AtResult` type alias. - Remove `AtResult` type alias.
- Update `toml` dependency to `0.8`. - Update `toml` dependency to `0.8`.

View File

@ -243,17 +243,13 @@ where
} }
/// Extension trait for applying parsed settings to the server object. /// Extension trait for applying parsed settings to the server object.
pub trait ApplySettings { pub trait ApplySettings<S> {
/// Apply a [`BasicSettings`] value to `self`. /// Apply some settings object value to `self`.
///
/// [`BasicSettings`]: ./struct.BasicSettings.html
#[must_use] #[must_use]
fn apply_settings<A>(self, settings: &BasicSettings<A>) -> Self fn apply_settings(self, settings: &S) -> Self;
where
A: de::DeserializeOwned;
} }
impl<F, I, S, B> ApplySettings for HttpServer<F, I, S, B> impl<F, I, S, B> ApplySettings<ActixSettings> for HttpServer<F, I, S, B>
where where
F: Fn() -> I + Send + Clone + 'static, F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request>, I: IntoServiceFactory<S, Request>,
@ -264,51 +260,48 @@ where
S::Future: 'static, S::Future: 'static,
B: MessageBody + 'static, B: MessageBody + 'static,
{ {
fn apply_settings<A>(mut self, settings: &BasicSettings<A>) -> Self fn apply_settings(mut self, settings: &ActixSettings) -> Self {
where if settings.tls.enabled {
A: de::DeserializeOwned,
{
if settings.actix.tls.enabled {
// for Address { host, port } in &settings.actix.hosts { // for Address { host, port } in &settings.actix.hosts {
// self = self.bind(format!("{}:{}", host, port)) // self = self.bind(format!("{}:{}", host, port))
// .unwrap(/*TODO*/); // .unwrap(/*TODO*/);
// } // }
todo!("[ApplySettings] TLS support has not been implemented yet."); todo!("[ApplySettings] TLS support has not been implemented yet.");
} else { } else {
for Address { host, port } in &settings.actix.hosts { for Address { host, port } in &settings.hosts {
self = self.bind(format!("{host}:{port}")) self = self.bind(format!("{host}:{port}"))
.unwrap(/*TODO*/); .unwrap(/*TODO*/);
} }
} }
self = match settings.actix.num_workers { self = match settings.num_workers {
NumWorkers::Default => self, NumWorkers::Default => self,
NumWorkers::Manual(n) => self.workers(n), NumWorkers::Manual(n) => self.workers(n),
}; };
self = match settings.actix.backlog { self = match settings.backlog {
Backlog::Default => self, Backlog::Default => self,
Backlog::Manual(n) => self.backlog(n as u32), Backlog::Manual(n) => self.backlog(n as u32),
}; };
self = match settings.actix.max_connections { self = match settings.max_connections {
MaxConnections::Default => self, MaxConnections::Default => self,
MaxConnections::Manual(n) => self.max_connections(n), MaxConnections::Manual(n) => self.max_connections(n),
}; };
self = match settings.actix.max_connection_rate { self = match settings.max_connection_rate {
MaxConnectionRate::Default => self, MaxConnectionRate::Default => self,
MaxConnectionRate::Manual(n) => self.max_connection_rate(n), MaxConnectionRate::Manual(n) => self.max_connection_rate(n),
}; };
self = match settings.actix.keep_alive { self = match settings.keep_alive {
KeepAlive::Default => self, KeepAlive::Default => self,
KeepAlive::Disabled => self.keep_alive(ActixKeepAlive::Disabled), KeepAlive::Disabled => self.keep_alive(ActixKeepAlive::Disabled),
KeepAlive::Os => self.keep_alive(ActixKeepAlive::Os), KeepAlive::Os => self.keep_alive(ActixKeepAlive::Os),
KeepAlive::Seconds(n) => self.keep_alive(Duration::from_secs(n as u64)), 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::Default => self,
Timeout::Milliseconds(n) => { Timeout::Milliseconds(n) => {
self.client_request_timeout(Duration::from_millis(n as u64)) 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)), 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::Default => self,
Timeout::Milliseconds(n) => { Timeout::Milliseconds(n) => {
self.client_disconnect_timeout(Duration::from_millis(n as u64)) 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)), 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::Default => self,
Timeout::Milliseconds(_) => self.shutdown_timeout(1), Timeout::Milliseconds(_) => self.shutdown_timeout(1),
Timeout::Seconds(n) => self.shutdown_timeout(n as u64), Timeout::Seconds(n) => self.shutdown_timeout(n as u64),
@ -334,6 +327,23 @@ where
} }
} }
impl<F, I, S, B, A> ApplySettings<BasicSettings<A>> for HttpServer<F, I, S, B>
where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request>,
S: ServiceFactory<Request, Config = AppConfig> + 'static,
S::Error: Into<WebError> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
A: de::DeserializeOwned,
{
fn apply_settings(self, settings: &BasicSettings<A>) -> Self {
self.apply_settings(&settings.actix)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use actix_web::App; use actix_web::App;