Skip to content

Commit

Permalink
refactor: [#599] extract types for config::v1::net::Network
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 23, 2024
1 parent c422e52 commit ab4717d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ impl Configuration {

pub async fn get_api_base_url(&self) -> Option<String> {
let settings_lock = self.settings.read().await;

settings_lock.net.base_url.clone()
settings_lock.net.base_url.as_ref().map(std::string::ToString::to_string)
}
}

Expand All @@ -338,6 +337,8 @@ fn parse_url(url_str: &str) -> Result<Url, url::ParseError> {
#[cfg(test)]
mod tests {

use url::Url;

use crate::config::v1::auth::SecretKey;
use crate::config::{Configuration, ConfigurationPublic, Info, Settings};

Expand Down Expand Up @@ -442,10 +443,10 @@ mod tests {
assert_eq!(configuration.get_api_base_url().await, None);

let mut settings_lock = configuration.settings.write().await;
settings_lock.net.base_url = Some("http://localhost".to_string());
settings_lock.net.base_url = Some(Url::parse("http://localhost").unwrap());
drop(settings_lock);

assert_eq!(configuration.get_api_base_url().await, Some("http://localhost".to_string()));
assert_eq!(configuration.get_api_base_url().await, Some("http://localhost/".to_string()));
}

#[tokio::test]
Expand Down
8 changes: 7 additions & 1 deletion src/config/v1/net.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use serde::{Deserialize, Serialize};
use url::Url;

use crate::config::Tsl;

/// The the base URL for the API.
///
/// NOTICE: that `port` and por in `base_url` does not necessarily match because
/// the application migth be running behind a proxy. The local socket could be
/// bound to, for example, port 80 but the application could be exposed publicly
/// via port 443, which is a very common setup.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Network {
/// The port to listen on. Default to `3001`.
pub port: u16,
/// The base URL for the API. For example: `http://localhost`.
/// If not set, the base URL will be inferred from the request.
pub base_url: Option<String>,
pub base_url: Option<Url>,
/// TSL configuration.
pub tsl: Option<Tsl>,
}
Expand Down
8 changes: 4 additions & 4 deletions src/mailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ impl Service {

let token = encode(&Header::default(), &claims, &EncodingKey::from_secret(key)).unwrap();

let mut base_url = &base_url.to_string();
if let Some(cfg_base_url) = &settings.net.base_url {
base_url = cfg_base_url;
}
let base_url = match &settings.net.base_url {
Some(url) => url.to_string(),
None => base_url.to_string(),
};

format!("{base_url}/{API_VERSION_URL_PREFIX}/user/email/verify/{token}")
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/api/client/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl From<DomainNetwork> for Network {
fn from(net: DomainNetwork) -> Self {
Self {
port: net.port,
base_url: net.base_url,
base_url: net.base_url.map(|url_without_port| url_without_port.to_string()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl From<DomainNetwork> for Network {
fn from(net: DomainNetwork) -> Self {
Self {
port: net.port,
base_url: net.base_url,
base_url: net.base_url.as_ref().map(std::string::ToString::to_string),
}
}
}
Expand Down

0 comments on commit ab4717d

Please sign in to comment.