Skip to content

Config overhaul: replace primitive types in configuration with richer types #852

@josecelano

Description

@josecelano

Parent issue: #401

Context

I was working on a configuration validation in this PR. I wanted to validate things like socket addresses earlier when we parse the configuration instead of waiting until the app launches the service. For example, the UDP tracker configuration contains the bind_address:

[[udp_trackers]]
bind_address = "0.0.0.0:6969"
enabled = false

That configuration maps to a String:

pub struct UdpTracker {
    pub enabled: bool,
    pub bind_address: String,
}

I realized that kind of very basic validation could be actually done just changing the types in the configuration. For example:

pub struct UdpTracker {
    pub enabled: bool,
    pub bind_address: ScoketAddr,
}

Pros:

  • Validation is done earlier
  • It simplifies the application when those values are used because they are already validated and we don't need to convert them every time we need them. For example, we wouldn't need to convert a string representing a socket address into a SocketAddr many times.

Changes

Configuration

Current:

pub struct Configuration {
    pub log_level: Option<String>,
    pub mode: TrackerMode,
    pub db_driver: DatabaseDriver,
    pub db_path: String,
    pub announce_interval: u32,
    pub min_announce_interval: u32,
    pub on_reverse_proxy: bool,
    pub external_ip: Option<String>,
    pub tracker_usage_statistics: bool,
    pub persistent_torrent_completed_stat: bool,
    pub max_peer_timeout: u32,
    pub inactive_peer_cleanup_interval: u64,
    pub remove_peerless_torrents: bool,
    pub udp_trackers: Vec<UdpTracker>,
    pub http_trackers: Vec<HttpTracker>,
    pub http_api: HttpApi,
    pub health_check_api: HealthCheckApi,
}

New:

pub struct Configuration {
    pub log_level: LevelFilter,
    pub mode: TrackerMode,
    pub db_driver: DatabaseDriver,
    pub db_path: PathBuf,
    pub announce_interval: u32,
    pub min_announce_interval: u32,
    pub on_reverse_proxy: bool,
    pub external_ip: Option<IpAddr>,
    pub tracker_usage_statistics: bool,
    pub persistent_torrent_completed_stat: bool,
    pub max_peer_timeout: u32,
    pub inactive_peer_cleanup_interval: u64,
    pub remove_peerless_torrents: bool,
    pub udp_trackers: Vec<UdpTracker>,
    pub http_trackers: Vec<HttpTracker>,
    pub http_api: HttpApi,
    pub health_check_api: HealthCheckApi,
}

HttpApi

Current:

pub struct HttpApi {
    pub enabled: bool,
    pub bind_address: String,
    pub ssl_enabled: bool,
    pub ssl_cert_path: Option<String>,
    pub ssl_key_path: Option<String>,
    pub access_tokens: AccessTokens,
}

New:

pub struct HttpApi {
    pub enabled: bool,
    pub bind_address: SocketAddr,
    pub ssl_enabled: bool,
    pub ssl_cert_path: Option<PathBuf>,
    pub ssl_key_path: Option<PathBuf>,
    pub access_tokens: AccessTokens,
}

UdpTracker

Current:

pub struct UdpTracker {
    pub enabled: bool,
    pub bind_address: String,
}

New:

pub struct UdpTracker {
    pub enabled: bool,
    pub bind_address: SocketAddr,
}

HttpTracker

Current:

pub struct HttpTracker {
    pub enabled: bool,
    pub bind_address: SocketAddr,
    pub ssl_enabled: bool,
    pub ssl_cert_path: Option<PathBuf>,
    pub ssl_key_path: Option<PathBuf>,
}

New:

pub struct HttpTracker {
    pub enabled: bool,
    pub bind_address: String,
    pub ssl_enabled: bool,
    pub ssl_cert_path: Option<PathBuf>,
    pub ssl_key_path: Option<PathBuf>,
}

HealthCheckApi

Current:

pub struct HealthCheckApi {
    pub bind_address: String,
}

New:

pub struct HealthCheckApi {
    pub bind_address: SocketAddr,
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions