Skip to content

Commit

Permalink
Allow ActixSettings to be applied to HttpServer in actix-settings (#321)
Browse files Browse the repository at this point in the history
* allow other settings objects to be applied

* update changelog

* update changelog

---------

Co-authored-by: LinuxHeki <[email protected]>
Co-authored-by: Rob Ede <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2023
1 parent 373a89a commit 76d9313
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
2 changes: 2 additions & 0 deletions actix-settings/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
54 changes: 32 additions & 22 deletions actix-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
/// Apply some settings object value to `self`.
#[must_use]
fn apply_settings<A>(self, settings: &BasicSettings<A>) -> Self
where
A: de::DeserializeOwned;
fn apply_settings(self, settings: &S) -> Self;
}

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
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request>,
Expand All @@ -264,67 +260,64 @@ where
S::Future: 'static,
B: MessageBody + 'static,
{
fn apply_settings<A>(mut self, settings: &BasicSettings<A>) -> 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))
}
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))
}
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),
Expand All @@ -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)]
mod tests {
use actix_web::App;
Expand Down

0 comments on commit 76d9313

Please sign in to comment.