Skip to content

Commit 445bd53

Browse files
committed
feat: define only non-defaults in toml config templates
After implementing the configuration with Figment, it's now possible to omit values if they have a default value. Therefore we don't need to add all options in templates. We only need to add values that are overwriting deffault values.
1 parent 3c78bba commit 445bd53

11 files changed

Lines changed: 169 additions & 185 deletions

packages/configuration/src/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,22 @@ pub struct AnnouncePolicy {
120120
impl Default for AnnouncePolicy {
121121
fn default() -> Self {
122122
Self {
123-
interval: 120,
124-
interval_min: 120,
123+
interval: Self::default_interval(),
124+
interval_min: Self::default_interval_min(),
125125
}
126126
}
127127
}
128128

129+
impl AnnouncePolicy {
130+
fn default_interval() -> u32 {
131+
120
132+
}
133+
134+
fn default_interval_min() -> u32 {
135+
120
136+
}
137+
}
138+
129139
/// Errors that can occur when loading the configuration.
130140
#[derive(Error, Debug)]
131141
pub enum Error {
@@ -166,12 +176,26 @@ impl From<figment::Error> for Error {
166176
pub struct TslConfig {
167177
/// Path to the SSL certificate file.
168178
#[serde_as(as = "NoneAsEmptyString")]
179+
#[serde(default = "TslConfig::default_ssl_cert_path")]
169180
pub ssl_cert_path: Option<Utf8PathBuf>,
170181
/// Path to the SSL key file.
171182
#[serde_as(as = "NoneAsEmptyString")]
183+
#[serde(default = "TslConfig::default_ssl_key_path")]
172184
pub ssl_key_path: Option<Utf8PathBuf>,
173185
}
174186

187+
impl TslConfig {
188+
#[allow(clippy::unnecessary_wraps)]
189+
fn default_ssl_cert_path() -> Option<Utf8PathBuf> {
190+
Some(Utf8PathBuf::new())
191+
}
192+
193+
#[allow(clippy::unnecessary_wraps)]
194+
fn default_ssl_key_path() -> Option<Utf8PathBuf> {
195+
Some(Utf8PathBuf::new())
196+
}
197+
}
198+
175199
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)]
176200
#[serde(rename_all = "lowercase")]
177201
pub enum LogLevel {

packages/configuration/src/v1/core.rs

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,71 @@ use crate::{AnnouncePolicy, LogLevel};
1010
pub struct Core {
1111
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
1212
/// `Debug` and `Trace`. Default is `Info`.
13+
#[serde(default = "Core::default_log_level")]
1314
pub log_level: Option<LogLevel>,
1415
/// Tracker mode. See [`TrackerMode`] for more information.
16+
#[serde(default = "Core::default_mode")]
1517
pub mode: TrackerMode,
1618

1719
// Database configuration
1820
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
21+
#[serde(default = "Core::default_db_driver")]
1922
pub db_driver: DatabaseDriver,
2023
/// Database connection string. The format depends on the database driver.
2124
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
2225
/// `./storage/tracker/lib/database/sqlite3.db`.
2326
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
2427
/// example: `root:password@localhost:3306/torrust`.
28+
#[serde(default = "Core::default_db_path")]
2529
pub db_path: String,
2630

2731
/// See [`AnnouncePolicy::interval`]
32+
#[serde(default = "AnnouncePolicy::default_interval")]
2833
pub announce_interval: u32,
2934

3035
/// See [`AnnouncePolicy::interval_min`]
36+
#[serde(default = "AnnouncePolicy::default_interval_min")]
3137
pub min_announce_interval: u32,
3238
/// Weather the tracker is behind a reverse proxy or not.
3339
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
3440
/// sent from the proxy will be used to get the client's IP address.
41+
#[serde(default = "Core::default_on_reverse_proxy")]
3542
pub on_reverse_proxy: bool,
3643
/// The external IP address of the tracker. If the client is using a
3744
/// loopback IP address, this IP address will be used instead. If the peer
3845
/// is using a loopback IP address, the tracker assumes that the peer is
3946
/// in the same network as the tracker and will use the tracker's IP
4047
/// address instead.
48+
#[serde(default = "Core::default_external_ip")]
4149
pub external_ip: Option<IpAddr>,
4250
/// Weather the tracker should collect statistics about tracker usage.
4351
/// If enabled, the tracker will collect statistics like the number of
4452
/// connections handled, the number of announce requests handled, etc.
4553
/// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more
4654
/// information about the collected metrics.
55+
#[serde(default = "Core::default_tracker_usage_statistics")]
4756
pub tracker_usage_statistics: bool,
4857
/// If enabled the tracker will persist the number of completed downloads.
4958
/// That's how many times a torrent has been downloaded completely.
59+
#[serde(default = "Core::default_persistent_torrent_completed_stat")]
5060
pub persistent_torrent_completed_stat: bool,
5161

5262
// Cleanup job configuration
5363
/// Maximum time in seconds that a peer can be inactive before being
5464
/// considered an inactive peer. If a peer is inactive for more than this
5565
/// time, it will be removed from the torrent peer list.
66+
#[serde(default = "Core::default_max_peer_timeout")]
5667
pub max_peer_timeout: u32,
5768
/// Interval in seconds that the cleanup job will run to remove inactive
5869
/// peers from the torrent peer list.
70+
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
5971
pub inactive_peer_cleanup_interval: u64,
6072
/// If enabled, the tracker will remove torrents that have no peers.
6173
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
6274
/// seconds and it removes inactive peers. Eventually, the peer list of a
6375
/// torrent could be empty and the torrent will be removed if this option is
6476
/// enabled.
77+
#[serde(default = "Core::default_remove_peerless_torrents")]
6578
pub remove_peerless_torrents: bool,
6679
}
6780

@@ -70,19 +83,67 @@ impl Default for Core {
7083
let announce_policy = AnnouncePolicy::default();
7184

7285
Self {
73-
log_level: Some(LogLevel::Info),
74-
mode: TrackerMode::Public,
75-
db_driver: DatabaseDriver::Sqlite3,
76-
db_path: String::from("./storage/tracker/lib/database/sqlite3.db"),
86+
log_level: Self::default_log_level(),
87+
mode: Self::default_mode(),
88+
db_driver: Self::default_db_driver(),
89+
db_path: Self::default_db_path(),
7790
announce_interval: announce_policy.interval,
7891
min_announce_interval: announce_policy.interval_min,
79-
max_peer_timeout: 900,
80-
on_reverse_proxy: false,
81-
external_ip: Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))),
82-
tracker_usage_statistics: true,
83-
persistent_torrent_completed_stat: false,
84-
inactive_peer_cleanup_interval: 600,
85-
remove_peerless_torrents: true,
92+
max_peer_timeout: Self::default_max_peer_timeout(),
93+
on_reverse_proxy: Self::default_on_reverse_proxy(),
94+
external_ip: Self::default_external_ip(),
95+
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
96+
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
97+
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
98+
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
8699
}
87100
}
88101
}
102+
103+
impl Core {
104+
#[allow(clippy::unnecessary_wraps)]
105+
fn default_log_level() -> Option<LogLevel> {
106+
Some(LogLevel::Info)
107+
}
108+
109+
fn default_mode() -> TrackerMode {
110+
TrackerMode::Public
111+
}
112+
113+
fn default_db_driver() -> DatabaseDriver {
114+
DatabaseDriver::Sqlite3
115+
}
116+
117+
fn default_db_path() -> String {
118+
String::from("./storage/tracker/lib/database/sqlite3.db")
119+
}
120+
121+
fn default_on_reverse_proxy() -> bool {
122+
false
123+
}
124+
125+
#[allow(clippy::unnecessary_wraps)]
126+
fn default_external_ip() -> Option<IpAddr> {
127+
Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
128+
}
129+
130+
fn default_tracker_usage_statistics() -> bool {
131+
true
132+
}
133+
134+
fn default_persistent_torrent_completed_stat() -> bool {
135+
false
136+
}
137+
138+
fn default_max_peer_timeout() -> u32 {
139+
900
140+
}
141+
142+
fn default_inactive_peer_cleanup_interval() -> u64 {
143+
600
144+
}
145+
146+
fn default_remove_peerless_torrents() -> bool {
147+
true
148+
}
149+
}

packages/configuration/src/v1/health_check_api.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ pub struct HealthCheckApi {
1111
/// The format is `ip:port`, for example `127.0.0.1:1313`. If you want to
1212
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1313
/// system to choose a random port, use port `0`.
14+
#[serde(default = "HealthCheckApi::default_bind_address")]
1415
pub bind_address: SocketAddr,
1516
}
1617

1718
impl Default for HealthCheckApi {
1819
fn default() -> Self {
1920
Self {
20-
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1313),
21+
bind_address: Self::default_bind_address(),
2122
}
2223
}
2324
}
25+
26+
impl HealthCheckApi {
27+
fn default_bind_address() -> SocketAddr {
28+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1313)
29+
}
30+
}

packages/configuration/src/v1/http_tracker.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,44 @@ use crate::TslConfig;
1010
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
1111
pub struct HttpTracker {
1212
/// Weather the HTTP tracker is enabled or not.
13+
#[serde(default = "HttpTracker::default_enabled")]
1314
pub enabled: bool,
1415
/// The address the tracker will bind to.
1516
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
1617
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1718
/// system to choose a random port, use port `0`.
19+
#[serde(default = "HttpTracker::default_bind_address")]
1820
pub bind_address: SocketAddr,
1921
/// Weather the HTTP tracker will use SSL or not.
22+
#[serde(default = "HttpTracker::default_ssl_enabled")]
2023
pub ssl_enabled: bool,
2124
/// TSL config. Only used if `ssl_enabled` is true.
2225
#[serde(flatten)]
26+
#[serde(default = "TslConfig::default")]
2327
pub tsl_config: TslConfig,
2428
}
2529

2630
impl Default for HttpTracker {
2731
fn default() -> Self {
2832
Self {
29-
enabled: false,
30-
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 7070),
31-
ssl_enabled: false,
33+
enabled: Self::default_enabled(),
34+
bind_address: Self::default_bind_address(),
35+
ssl_enabled: Self::default_ssl_enabled(),
3236
tsl_config: TslConfig::default(),
3337
}
3438
}
3539
}
40+
41+
impl HttpTracker {
42+
fn default_enabled() -> bool {
43+
false
44+
}
45+
46+
fn default_bind_address() -> SocketAddr {
47+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 7070)
48+
}
49+
50+
fn default_ssl_enabled() -> bool {
51+
false
52+
}
53+
}

packages/configuration/src/v1/tracker_api.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,61 @@ pub type AccessTokens = HashMap<String, String>;
1313
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
1414
pub struct HttpApi {
1515
/// Weather the HTTP API is enabled or not.
16+
#[serde(default = "HttpApi::default_enabled")]
1617
pub enabled: bool,
1718
/// The address the tracker will bind to.
1819
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
1920
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
2021
/// system to choose a random port, use port `0`.
22+
#[serde(default = "HttpApi::default_bind_address")]
2123
pub bind_address: SocketAddr,
2224
/// Weather the HTTP API will use SSL or not.
25+
#[serde(default = "HttpApi::default_ssl_enabled")]
2326
pub ssl_enabled: bool,
2427
/// TSL config. Only used if `ssl_enabled` is true.
2528
#[serde(flatten)]
29+
#[serde(default = "TslConfig::default")]
2630
pub tsl_config: TslConfig,
2731
/// Access tokens for the HTTP API. The key is a label identifying the
2832
/// token and the value is the token itself. The token is used to
2933
/// authenticate the user. All tokens are valid for all endpoints and have
3034
/// all permissions.
35+
#[serde(default = "HttpApi::default_access_tokens")]
3136
pub access_tokens: AccessTokens,
3237
}
3338

3439
impl Default for HttpApi {
3540
fn default() -> Self {
3641
Self {
37-
enabled: true,
38-
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1212),
39-
ssl_enabled: false,
42+
enabled: Self::default_enabled(),
43+
bind_address: Self::default_bind_address(),
44+
ssl_enabled: Self::default_ssl_enabled(),
4045
tsl_config: TslConfig::default(),
41-
access_tokens: [(String::from("admin"), String::from("MyAccessToken"))]
42-
.iter()
43-
.cloned()
44-
.collect(),
46+
access_tokens: Self::default_access_tokens(),
4547
}
4648
}
4749
}
4850

4951
impl HttpApi {
52+
fn default_enabled() -> bool {
53+
true
54+
}
55+
56+
fn default_bind_address() -> SocketAddr {
57+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1212)
58+
}
59+
60+
fn default_ssl_enabled() -> bool {
61+
false
62+
}
63+
64+
fn default_access_tokens() -> AccessTokens {
65+
[(String::from("admin"), String::from("MyAccessToken"))]
66+
.iter()
67+
.cloned()
68+
.collect()
69+
}
70+
5071
pub fn override_admin_token(&mut self, api_admin_token: &str) {
5172
self.access_tokens.insert("admin".to_string(), api_admin_token.to_string());
5273
}

packages/configuration/src/v1/udp_tracker.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ use serde::{Deserialize, Serialize};
55
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
66
pub struct UdpTracker {
77
/// Weather the UDP tracker is enabled or not.
8+
#[serde(default = "UdpTracker::default_enabled")]
89
pub enabled: bool,
910
/// The address the tracker will bind to.
1011
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
1112
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1213
/// system to choose a random port, use port `0`.
14+
#[serde(default = "UdpTracker::default_bind_address")]
1315
pub bind_address: SocketAddr,
1416
}
1517
impl Default for UdpTracker {
1618
fn default() -> Self {
1719
Self {
18-
enabled: false,
19-
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 6969),
20+
enabled: Self::default_enabled(),
21+
bind_address: Self::default_bind_address(),
2022
}
2123
}
2224
}
25+
26+
impl UdpTracker {
27+
fn default_enabled() -> bool {
28+
false
29+
}
30+
31+
fn default_bind_address() -> SocketAddr {
32+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 6969)
33+
}
34+
}

0 commit comments

Comments
 (0)