Skip to content

Commit

Permalink
feat: [#603] add default values to configration
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 24, 2024
1 parent a1b5350 commit 56a8c08
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 35 deletions.
16 changes: 14 additions & 2 deletions src/config/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
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,
}

impl Default for Api {
fn default() -> Self {
Self {
default_torrent_page_size: 10,
max_torrent_page_size: 30,
default_torrent_page_size: Api::default_default_torrent_page_size(),
max_torrent_page_size: Api::default_max_torrent_page_size(),
}
}
}

impl Api {
fn default_default_torrent_page_size() -> u8 {
10
}

fn default_max_torrent_page_size() -> u8 {
30
}
}
22 changes: 19 additions & 3 deletions src/config/v1/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Auth {
/// Whether or not to require an email on signup.
#[serde(default = "EmailOnSignup::default")]
pub email_on_signup: EmailOnSignup,
/// The minimum password length.
#[serde(default = "Auth::default_min_password_length")]
pub min_password_length: usize,
/// The maximum password length.
#[serde(default = "Auth::default_max_password_length")]
pub max_password_length: usize,
/// The secret key used to sign JWT tokens.
#[serde(default = "Auth::default_secret_key")]
pub secret_key: SecretKey,
}

impl Default for Auth {
fn default() -> Self {
Self {
email_on_signup: EmailOnSignup::default(),
min_password_length: 6,
max_password_length: 64,
secret_key: SecretKey::new("MaxVerstappenWC2021"),
min_password_length: Self::default_min_password_length(),
max_password_length: Self::default_max_password_length(),
secret_key: Self::default_secret_key(),
}
}
}
Expand All @@ -30,6 +34,18 @@ impl Auth {
pub fn override_secret_key(&mut self, secret_key: &str) {
self.secret_key = SecretKey::new(secret_key);
}

fn default_min_password_length() -> usize {
6
}

fn default_max_password_length() -> usize {
64
}

fn default_secret_key() -> SecretKey {
SecretKey::new("MaxVerstappenWC2021")
}
}

/// Whether the email is required on signup or not.
Expand Down
9 changes: 8 additions & 1 deletion src/config/v1/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ pub struct Database {
///
/// Sqlite: `sqlite://data.db?mode=rwc`.
/// Mysql: `mysql://root:root_secret_password@mysql:3306/torrust_index_e2e_testing`.
#[serde(default = "Database::default_connect_url")]
pub connect_url: Url,
}

impl Default for Database {
fn default() -> Self {
Self {
connect_url: Url::parse("sqlite://data.db?mode=rwc").unwrap(),
connect_url: Self::default_connect_url(),
}
}
}

impl Database {
fn default_connect_url() -> Url {
Url::parse("sqlite://data.db?mode=rwc").unwrap()
}
}
37 changes: 32 additions & 5 deletions src/config/v1/image_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,54 @@ use serde::{Deserialize, Serialize};
#[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,
/// 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,
}

impl Default for ImageCache {
fn default() -> Self {
Self {
max_request_timeout_ms: 1000,
capacity: 128_000_000,
entry_size_limit: 4_000_000,
user_quota_period_seconds: 3600,
user_quota_bytes: 64_000_000,
max_request_timeout_ms: Self::default_max_request_timeout_ms(),
capacity: Self::default_capacity(),
entry_size_limit: Self::default_entry_size_limit(),
user_quota_period_seconds: Self::default_user_quota_period_seconds(),
user_quota_bytes: Self::default_user_quota_bytes(),
}
}
}

impl ImageCache {
fn default_max_request_timeout_ms() -> u64 {
1000
}

fn default_capacity() -> usize {
128_000_000
}

fn default_entry_size_limit() -> usize {
4_000_000
}

fn default_user_quota_period_seconds() -> u64 {
3600
}

fn default_user_quota_bytes() -> usize {
64_000_000
}
}
51 changes: 44 additions & 7 deletions src/config/v1/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,68 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
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 username to use for SMTP authentication.
#[serde(default = "Mail::default_username")]
pub username: String,
/// The password to use for SMTP authentication.
#[serde(default = "Mail::default_password")]
pub password: String,
/// The SMTP server to use.
#[serde(default = "Mail::default_server")]
pub server: String,
/// The SMTP port to use.
#[serde(default = "Mail::default_port")]
pub port: u16,
}

impl Default for Mail {
fn default() -> Self {
Self {
email_verification_enabled: false,
from: "example@email.com".parse().expect("valid mailbox"),
reply_to: "noreply@email.com".parse().expect("valid mailbox"),
username: String::default(),
password: String::default(),
server: String::default(),
port: 25,
email_verification_enabled: Self::default_email_verification_enabled(),
from: Self::default_from(),
reply_to: Self::default_reply_to(),
username: Self::default_username(),
password: Self::default_password(),
server: Self::default_server(),
port: Self::default_port(),
}
}
}

impl Mail {
fn default_email_verification_enabled() -> bool {
false
}

fn default_from() -> Mailbox {
"example@email.com".parse().expect("valid mailbox")
}

fn default_reply_to() -> Mailbox {
"noreply@email.com".parse().expect("valid mailbox")
}

fn default_username() -> String {
String::default()
}

fn default_password() -> String {
String::default()
}

fn default_server() -> String {
String::default()
}

fn default_port() -> u16 {
25
}
}
10 changes: 10 additions & 0 deletions src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,34 @@ use super::validator::{ValidationError, Validator};
pub struct Settings {
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
/// `Debug` and `Trace`. Default is `Info`.
#[serde(default)]
pub log_level: Option<LogLevel>,
/// 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
23 changes: 20 additions & 3 deletions src/config/v1/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ use crate::config::Tsl;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Network {
/// The port to listen on. Default to `3001`.
#[serde(default = "Network::default_port")]
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.
#[serde(default = "Network::default_base_url")]
pub base_url: Option<Url>,
/// TSL configuration.
#[serde(default = "Network::default_tsl")]
pub tsl: Option<Tsl>,
}

impl Default for Network {
fn default() -> Self {
Self {
port: 3001,
base_url: None,
tsl: None,
port: Self::default_port(),
base_url: Self::default_base_url(),
tsl: Self::default_tsl(),
}
}
}

impl Network {
fn default_port() -> u16 {
3001
}

fn default_base_url() -> Option<Url> {
None
}

fn default_tsl() -> Option<Tsl> {
None
}
}
47 changes: 36 additions & 11 deletions src/config/v1/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ use crate::config::TrackerMode;
#[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 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,
}

impl Tracker {
pub fn override_tracker_api_token(&mut self, tracker_api_token: &ApiToken) {
self.token = tracker_api_token.clone();
}
}

impl Validator for Tracker {
fn validate(&self) -> Result<(), ValidationError> {
let tracker_mode = self.mode.clone();
Expand All @@ -45,15 +44,41 @@ impl Validator for Tracker {
impl Default for Tracker {
fn default() -> Self {
Self {
url: Url::parse("udp://localhost:6969").unwrap(),
mode: TrackerMode::default(),
api_url: Url::parse("http://localhost:1212/").unwrap(),
token: ApiToken::new("MyAccessToken"),
token_valid_seconds: 7_257_600,
url: Self::default_url(),
mode: Self::default_mode(),
api_url: Self::default_api_url(),
token: Self::default_token(),
token_valid_seconds: Self::default_token_valid_seconds(),
}
}
}

impl Tracker {
pub fn override_tracker_api_token(&mut self, tracker_api_token: &ApiToken) {
self.token = tracker_api_token.clone();
}

fn default_url() -> Url {
Url::parse("udp://localhost:6969").unwrap()
}

fn default_mode() -> TrackerMode {
TrackerMode::default()
}

fn default_api_url() -> Url {
Url::parse("http://localhost:1212/").unwrap()
}

fn default_token() -> ApiToken {
ApiToken::new("MyAccessToken")
}

fn default_token_valid_seconds() -> u64 {
7_257_600
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ApiToken(String);

Expand Down
Loading

0 comments on commit 56a8c08

Please sign in to comment.