torrust_index/config/v2/
mod.rs

1pub mod api;
2pub mod auth;
3pub mod database;
4pub mod image_cache;
5pub mod logging;
6pub mod mail;
7pub mod net;
8pub mod registration;
9pub mod tracker;
10pub mod tracker_statistics_importer;
11pub mod unstable;
12pub mod website;
13
14use logging::Logging;
15use registration::Registration;
16use serde::{Deserialize, Serialize};
17use unstable::Unstable;
18
19use self::api::Api;
20use self::auth::{Auth, ClaimTokenPepper};
21use self::database::Database;
22use self::image_cache::ImageCache;
23use self::mail::Mail;
24use self::net::Network;
25use self::tracker::{ApiToken, Tracker};
26use self::tracker_statistics_importer::TrackerStatisticsImporter;
27use self::website::Website;
28use super::validator::{ValidationError, Validator};
29use super::Metadata;
30
31/// The whole configuration for the index.
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
33pub struct Settings {
34    /// Configuration metadata.
35    #[serde(default = "Settings::default_metadata")]
36    pub metadata: Metadata,
37
38    /// The logging configuration.
39    #[serde(default = "Settings::default_logging")]
40    pub logging: Logging,
41
42    /// The website customizable values.
43    #[serde(default = "Settings::default_website")]
44    pub website: Website,
45
46    /// The tracker configuration.
47    #[serde(default = "Settings::default_tracker")]
48    pub tracker: Tracker,
49
50    /// The network configuration.
51    #[serde(default = "Settings::default_network")]
52    pub net: Network,
53
54    /// The authentication configuration.
55    #[serde(default = "Settings::default_auth")]
56    pub auth: Auth,
57
58    /// The database configuration.
59    #[serde(default = "Settings::default_database")]
60    pub database: Database,
61
62    /// The SMTP configuration.
63    #[serde(default = "Settings::default_mail")]
64    pub mail: Mail,
65
66    /// The image proxy cache configuration.
67    #[serde(default = "Settings::default_image_cache")]
68    pub image_cache: ImageCache,
69
70    /// The API configuration.
71    #[serde(default = "Settings::default_api")]
72    pub api: Api,
73
74    /// The registration configuration.
75    #[serde(default = "Settings::default_registration")]
76    pub registration: Option<Registration>,
77
78    /// The tracker statistics importer job configuration.
79    #[serde(default = "Settings::default_tracker_statistics_importer")]
80    pub tracker_statistics_importer: TrackerStatisticsImporter,
81
82    /// The unstable configuration.
83    #[serde(default = "Settings::default_unstable")]
84    pub unstable: Option<Unstable>,
85}
86
87impl Default for Settings {
88    fn default() -> Self {
89        Self {
90            metadata: Self::default_metadata(),
91            logging: Self::default_logging(),
92            website: Self::default_website(),
93            tracker: Self::default_tracker(),
94            net: Self::default_network(),
95            auth: Self::default_auth(),
96            database: Self::default_database(),
97            mail: Self::default_mail(),
98            image_cache: Self::default_image_cache(),
99            api: Self::default_api(),
100            registration: Self::default_registration(),
101            tracker_statistics_importer: Self::default_tracker_statistics_importer(),
102            unstable: Self::default_unstable(),
103        }
104    }
105}
106
107impl Settings {
108    pub fn remove_secrets(&mut self) {
109        self.tracker.token = ApiToken::new("***");
110        if let Some(_password) = self.database.connect_url.password() {
111            let _ = self.database.connect_url.set_password(Some("***"));
112        }
113        "***".clone_into(&mut self.mail.smtp.credentials.password);
114        self.auth.user_claim_token_pepper = ClaimTokenPepper::new("***");
115    }
116
117    /// Encodes the configuration to TOML.
118    ///
119    /// # Panics
120    ///
121    /// Will panic if it can't be converted to TOML.
122    #[must_use]
123    pub fn to_toml(&self) -> String {
124        toml::to_string(self).expect("Could not encode TOML value")
125    }
126
127    /// Encodes the configuration to JSON.
128    ///
129    /// # Panics
130    ///
131    /// Will panic if it can't be converted to JSON.
132    #[must_use]
133    pub fn to_json(&self) -> String {
134        serde_json::to_string_pretty(self).expect("Could not encode JSON value")
135    }
136
137    fn default_metadata() -> Metadata {
138        Metadata::default()
139    }
140
141    fn default_logging() -> Logging {
142        Logging::default()
143    }
144
145    fn default_website() -> Website {
146        Website::default()
147    }
148
149    fn default_tracker() -> Tracker {
150        Tracker::default()
151    }
152
153    fn default_network() -> Network {
154        Network::default()
155    }
156
157    fn default_auth() -> Auth {
158        Auth::default()
159    }
160
161    fn default_database() -> Database {
162        Database::default()
163    }
164
165    fn default_mail() -> Mail {
166        Mail::default()
167    }
168
169    fn default_image_cache() -> ImageCache {
170        ImageCache::default()
171    }
172
173    fn default_api() -> Api {
174        Api::default()
175    }
176
177    fn default_registration() -> Option<Registration> {
178        None
179    }
180
181    fn default_tracker_statistics_importer() -> TrackerStatisticsImporter {
182        TrackerStatisticsImporter::default()
183    }
184
185    fn default_unstable() -> Option<Unstable> {
186        None
187    }
188}
189
190impl Validator for Settings {
191    fn validate(&self) -> Result<(), ValidationError> {
192        self.tracker.validate()
193    }
194}