pub struct HttpApiConfig { /* private fields */ }Expand description
HTTP API configuration with domain invariants enforced at construction
This type guarantees that any instance is valid according to domain rules:
- Bind address has a non-zero port
- If TLS proxy is enabled, a domain is configured
- If TLS proxy is enabled, bind address is not localhost
§Construction
Use HttpApiConfig::new() to create instances with validation:
use torrust_tracker_deployer_lib::domain::tracker::HttpApiConfig;
use torrust_tracker_deployer_lib::shared::ApiToken;
let config = HttpApiConfig::new(
"0.0.0.0:1212".parse().unwrap(),
ApiToken::from("MyToken".to_string()),
None,
false,
)?;§Invariants
The following invariants are enforced at construction time:
- No dynamic ports:
bind_address.port() != 0 - TLS requires domain:
use_tls_proxy == trueimpliesdomain.is_some() - No localhost with TLS:
use_tls_proxy == trueimplies!is_localhost(bind_address)
Implementations§
Source§impl HttpApiConfig
impl HttpApiConfig
Sourcepub fn new(
bind_address: SocketAddr,
admin_token: ApiToken,
domain: Option<DomainName>,
use_tls_proxy: bool,
) -> Result<Self, HttpApiConfigError>
pub fn new( bind_address: SocketAddr, admin_token: ApiToken, domain: Option<DomainName>, use_tls_proxy: bool, ) -> Result<Self, HttpApiConfigError>
Creates a new HTTP API configuration with validation
This is the primary way to construct an HttpApiConfig. All domain
invariants are validated before the instance is created.
§Arguments
bind_address- Socket address to bind to (e.g., “0.0.0.0:1212”)admin_token- API token for authenticationdomain- Optional domain for TLS certificate (required ifuse_tls_proxyis true)use_tls_proxy- Whether to enable TLS via Caddy reverse proxy
§Errors
Returns HttpApiConfigError if any invariant is violated:
DynamicPortNotSupported- if port is 0TlsProxyRequiresDomain- ifuse_tls_proxyis true butdomainis NoneLocalhostWithTls- ifuse_tls_proxyis true andbind_addressis localhost
§Examples
use torrust_tracker_deployer_lib::domain::tracker::HttpApiConfig;
use torrust_tracker_deployer_lib::shared::{ApiToken, DomainName};
// Basic configuration without TLS
let config = HttpApiConfig::new(
"0.0.0.0:1212".parse().unwrap(),
ApiToken::from("MyToken".to_string()),
None,
false,
)?;
// Configuration with TLS (requires domain)
let tls_config = HttpApiConfig::new(
"0.0.0.0:1212".parse().unwrap(),
ApiToken::from("MyToken".to_string()),
Some(DomainName::new("api.example.com")?),
true,
)?;Sourcepub fn bind_address(&self) -> SocketAddr
pub fn bind_address(&self) -> SocketAddr
Returns the bind address
Sourcepub fn admin_token(&self) -> &ApiToken
pub fn admin_token(&self) -> &ApiToken
Returns a reference to the admin token
Sourcepub fn domain(&self) -> Option<&DomainName>
pub fn domain(&self) -> Option<&DomainName>
Returns a reference to the domain, if configured
Sourcepub fn use_tls_proxy(&self) -> bool
pub fn use_tls_proxy(&self) -> bool
Returns whether TLS proxy is enabled
Sourcepub fn uses_tls_proxy(&self) -> bool
pub fn uses_tls_proxy(&self) -> bool
Returns true if this API uses the TLS proxy
Alias for use_tls_proxy() for semantic clarity.
Sourcepub fn tls_domain(&self) -> Option<&DomainName>
pub fn tls_domain(&self) -> Option<&DomainName>
Returns the domain name if TLS proxy is enabled
Returns None if TLS is disabled, even if a domain is configured.
This is useful for determining the effective TLS domain.
Trait Implementations§
Source§impl Clone for HttpApiConfig
impl Clone for HttpApiConfig
Source§impl Debug for HttpApiConfig
impl Debug for HttpApiConfig
Source§impl<'de> Deserialize<'de> for HttpApiConfig
Enables deserialization with validation through TryFrom
impl<'de> Deserialize<'de> for HttpApiConfig
Enables deserialization with validation through TryFrom
This ensures that JSON deserialization also validates the config, maintaining the “always valid” invariant even for loaded data.
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for HttpApiConfig
impl Display for HttpApiConfig
Source§impl PartialEq for HttpApiConfig
impl PartialEq for HttpApiConfig
Source§impl Serialize for HttpApiConfig
impl Serialize for HttpApiConfig
impl StructuralPartialEq for HttpApiConfig
Source§impl TryFrom<HttpApiSection> for HttpApiConfig
Converts HttpApiSection (DTO) to HttpApiConfig (Domain)
impl TryFrom<HttpApiSection> for HttpApiConfig
Converts HttpApiSection (DTO) to HttpApiConfig (Domain)
This implementation:
- Parses the bind address string to
SocketAddr - Parses the domain string to
DomainName(if present) - Delegates business validation to
HttpApiConfig::new()
§Errors
Returns CreateConfigError:
InvalidBindAddress- if bind address cannot be parsed as IP:PORTInvalidDomain- if domain string is not a valid domain nameHttpApiConfigInvalid- if domain invariants are violated (port 0, TLS without domain, etc.)
§Example
use torrust_tracker_deployer_lib::application::command_handlers::create::config::tracker::HttpApiSection;
use torrust_tracker_deployer_lib::domain::tracker::HttpApiConfig;
let section = HttpApiSection {
bind_address: "0.0.0.0:1212".to_string(),
admin_token: "MyToken".to_string(),
domain: None,
use_tls_proxy: None,
};
let config: HttpApiConfig = section.try_into()?;
assert_eq!(config.bind_address().port(), 1212);Source§type Error = CreateConfigError
type Error = CreateConfigError
Auto Trait Implementations§
impl Freeze for HttpApiConfig
impl RefUnwindSafe for HttpApiConfig
impl Send for HttpApiConfig
impl Sync for HttpApiConfig
impl Unpin for HttpApiConfig
impl UnsafeUnpin for HttpApiConfig
impl UnwindSafe for HttpApiConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = !
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.