Skip to content

Commit

Permalink
refactor: [#591] order keys in config TOML files alphabetically
Browse files Browse the repository at this point in the history
We should produce always the same TOML file from the same configuration
deterministically.
  • Loading branch information
josecelano committed Jun 13, 2024
1 parent cb8935b commit 35a125e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,11 @@ mod tests {
name = "Torrust"
[tracker]
url = "udp://localhost:6969"
mode = "public"
api_url = "http://localhost:1212/"
mode = "public"
token = "MyAccessToken"
token_valid_seconds = 7257600
url = "udp://localhost:6969"
[net]
bind_address = "0.0.0.0:3001"
Expand All @@ -361,9 +361,9 @@ mod tests {
secret_key = "MaxVerstappenWC2021"
[auth.password_constraints]
min_password_length = 6
max_password_length = 64
min_password_length = 6
[database]
connect_url = "sqlite://data.db?mode=rwc"
Expand All @@ -381,19 +381,19 @@ mod tests {
username = ""
[image_cache]
max_request_timeout_ms = 1000
capacity = 128000000
entry_size_limit = 4000000
user_quota_period_seconds = 3600
max_request_timeout_ms = 1000
user_quota_bytes = 64000000
user_quota_period_seconds = 3600
[api]
default_torrent_page_size = 10
max_torrent_page_size = 30
[tracker_statistics_importer]
torrent_info_update_interval = 3600
port = 3002
torrent_info_update_interval = 3600
"#
.lines()
.map(str::trim_start)
Expand Down
1 change: 1 addition & 0 deletions src/config/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Api {
/// The default page size for torrent lists.
#[serde(default = "Api::default_default_torrent_page_size")]
pub default_torrent_page_size: u8,

/// The maximum page size for torrent lists.
#[serde(default = "Api::default_max_torrent_page_size")]
pub max_torrent_page_size: u8,
Expand Down
10 changes: 6 additions & 4 deletions src/config/v1/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ pub struct Auth {
/// Whether or not to require an email on signup.
#[serde(default = "Auth::default_email_on_signup")]
pub email_on_signup: EmailOnSignup,

/// The secret key used to sign JWT tokens.
#[serde(default = "Auth::default_secret_key")]
pub secret_key: SecretKey,

/// The password constraints
#[serde(default = "Auth::default_password_constraints")]
pub password_constraints: PasswordConstraints,
Expand Down Expand Up @@ -117,19 +119,19 @@ impl fmt::Display for SecretKey {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PasswordConstraints {
/// The minimum password length.
#[serde(default = "PasswordConstraints::default_min_password_length")]
pub min_password_length: usize,
/// The maximum password length.
#[serde(default = "PasswordConstraints::default_max_password_length")]
pub max_password_length: usize,
/// The minimum password length.
#[serde(default = "PasswordConstraints::default_min_password_length")]
pub min_password_length: usize,
}

impl Default for PasswordConstraints {
fn default() -> Self {
Self {
min_password_length: Self::default_min_password_length(),
max_password_length: Self::default_max_password_length(),
min_password_length: Self::default_min_password_length(),
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/config/v1/image_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ use serde::{Deserialize, Serialize};
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImageCache {
/// Maximum time in seconds to wait for downloading the image form the original source.
#[serde(default = "ImageCache::default_max_request_timeout_ms")]
pub max_request_timeout_ms: u64,
/// Cache size in bytes.
#[serde(default = "ImageCache::default_capacity")]
pub capacity: usize,

/// Maximum size in bytes for a single image.
#[serde(default = "ImageCache::default_entry_size_limit")]
pub entry_size_limit: usize,
/// Users have a cache quota per period. For example: 100MB per day.
/// This is the period in seconds (1 day in seconds).
#[serde(default = "ImageCache::default_user_quota_period_seconds")]
pub user_quota_period_seconds: u64,

/// Maximum time in seconds to wait for downloading the image form the original source.
#[serde(default = "ImageCache::default_max_request_timeout_ms")]
pub max_request_timeout_ms: u64,

/// Users have a cache quota per period. For example: 100MB per day.
/// This is the maximum size in bytes (100MB in bytes).
#[serde(default = "ImageCache::default_user_quota_bytes")]
pub user_quota_bytes: usize,

/// Users have a cache quota per period. For example: 100MB per day.
/// This is the period in seconds (1 day in seconds).
#[serde(default = "ImageCache::default_user_quota_period_seconds")]
pub user_quota_period_seconds: u64,
}

impl Default for ImageCache {
Expand Down
3 changes: 3 additions & 0 deletions src/config/v1/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ pub struct Mail {
/// Whether or not to enable email verification on signup.
#[serde(default = "Mail::default_email_verification_enabled")]
pub email_verification_enabled: bool,

/// The email address to send emails from.
#[serde(default = "Mail::default_from")]
pub from: Mailbox,

/// The email address to reply to.
#[serde(default = "Mail::default_reply_to")]
pub reply_to: Mailbox,

/// The SMTP server configuration.
#[serde(default = "Mail::default_smtp")]
pub smtp: Smtp,
Expand Down
9 changes: 9 additions & 0 deletions src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,39 @@ pub struct Settings {
/// The logging configuration.
#[serde(default)]
pub logging: Logging,

/// The website customizable values.
#[serde(default)]
pub website: Website,

/// The tracker configuration.
#[serde(default)]
pub tracker: Tracker,

/// The network configuration.
#[serde(default)]
pub net: Network,

/// The authentication configuration.
#[serde(default)]
pub auth: Auth,

/// The database configuration.
#[serde(default)]
pub database: Database,

/// The SMTP configuration.
#[serde(default)]
pub mail: Mail,

/// The image proxy cache configuration.
#[serde(default)]
pub image_cache: ImageCache,

/// The API configuration.
#[serde(default)]
pub api: Api,

/// The tracker statistics importer job configuration.
#[serde(default)]
pub tracker_statistics_importer: TrackerStatisticsImporter,
Expand Down
2 changes: 2 additions & 0 deletions src/config/v1/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ pub struct Network {
/// If not set, the base URL will be inferred from the request.
#[serde(default = "Network::default_base_url")]
pub base_url: Option<Url>,

/// The address the tracker will bind to.
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
/// system to choose a random port, use port `0`.
#[serde(default = "Network::default_bind_address")]
pub bind_address: SocketAddr,

/// TSL configuration.
#[serde(default = "Network::default_tsl")]
pub tsl: Option<Tsl>,
Expand Down
16 changes: 10 additions & 6 deletions src/config/v1/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ use crate::config::TrackerMode;
/// Configuration for the associated tracker.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Tracker {
/// Connection string for the tracker. For example: `udp://TRACKER_IP:6969`.
#[serde(default = "Tracker::default_url")]
pub url: Url,
/// The url of the tracker API. For example: `http://localhost:1212/`.
#[serde(default = "Tracker::default_api_url")]
pub api_url: Url,

/// The mode of the tracker. For example: `Public`.
/// See `TrackerMode` in [`torrust-tracker-primitives`](https://docs.rs/torrust-tracker-primitives)
/// crate for more information.
#[serde(default = "Tracker::default_mode")]
pub mode: TrackerMode,
/// The url of the tracker API. For example: `http://localhost:1212/`.
#[serde(default = "Tracker::default_api_url")]
pub api_url: Url,

/// The token used to authenticate with the tracker API.
#[serde(default = "Tracker::default_token")]
pub token: ApiToken,

/// The amount of seconds the tracker API token is valid.
#[serde(default = "Tracker::default_token_valid_seconds")]
pub token_valid_seconds: u64,

/// Connection string for the tracker. For example: `udp://TRACKER_IP:6969`.
#[serde(default = "Tracker::default_url")]
pub url: Url,
}

impl Validator for Tracker {
Expand Down
7 changes: 4 additions & 3 deletions src/config/v1/tracker_statistics_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use serde::{Deserialize, Serialize};
/// Configuration for the tracker statistics importer.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrackerStatisticsImporter {
/// The interval in seconds to get statistics from the tracker.
#[serde(default = "TrackerStatisticsImporter::default_torrent_info_update_interval")]
pub torrent_info_update_interval: u64,
/// The port the Importer API is listening on. Default to `3002`.
#[serde(default = "TrackerStatisticsImporter::default_port")]
pub port: u16,

/// The interval in seconds to get statistics from the tracker.
#[serde(default = "TrackerStatisticsImporter::default_torrent_info_update_interval")]
pub torrent_info_update_interval: u64,
}

impl Default for TrackerStatisticsImporter {
Expand Down

0 comments on commit 35a125e

Please sign in to comment.