Skip to content

Commit 6c98e2b

Browse files
committed
refactor: [#589] versioning for configuration
1 parent d7d6d88 commit 6c98e2b

File tree

11 files changed

+330
-282
lines changed

11 files changed

+330
-282
lines changed

src/config.rs renamed to src/config/mod.rs

Lines changed: 14 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Configuration for the application.
2+
pub mod v1;
3+
24
use std::sync::Arc;
35
use std::{env, fs};
46

@@ -11,6 +13,18 @@ use tokio::sync::RwLock;
1113
use torrust_index_located_error::{Located, LocatedError};
1214
use url::{ParseError, Url};
1315

16+
pub type TorrustIndex = v1::TorrustIndex;
17+
pub type Api = v1::api::Api;
18+
pub type Auth = v1::auth::Auth;
19+
pub type Database = v1::database::Database;
20+
pub type ImageCache = v1::image_cache::ImageCache;
21+
pub type Mail = v1::mail::Mail;
22+
pub type Network = v1::net::Network;
23+
pub type TrackerStatisticsImporter = v1::tracker_statistics_importer::TrackerStatisticsImporter;
24+
pub type Tracker = v1::tracker::Tracker;
25+
pub type Website = v1::website::Website;
26+
pub type EmailOnSignup = v1::auth::EmailOnSignup;
27+
1428
/// Information required for loading config
1529
#[derive(Debug, Default, Clone)]
1630
pub struct Info {
@@ -120,21 +134,6 @@ impl From<ConfigError> for Error {
120134
}
121135
}
122136

123-
/// Information displayed to the user in the website.
124-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
125-
pub struct Website {
126-
/// The name of the website.
127-
pub name: String,
128-
}
129-
130-
impl Default for Website {
131-
fn default() -> Self {
132-
Self {
133-
name: "Torrust".to_string(),
134-
}
135-
}
136-
}
137-
138137
/// See `TrackerMode` in [`torrust-tracker-primitives`](https://docs.rs/torrust-tracker-primitives)
139138
/// crate for more information.
140139
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -168,235 +167,11 @@ impl TrackerMode {
168167
}
169168
}
170169

171-
/// Configuration for the associated tracker.
172-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
173-
pub struct Tracker {
174-
/// Connection string for the tracker. For example: `udp://TRACKER_IP:6969`.
175-
pub url: String,
176-
/// The mode of the tracker. For example: `Public`.
177-
/// See `TrackerMode` in [`torrust-tracker-primitives`](https://docs.rs/torrust-tracker-primitives)
178-
/// crate for more information.
179-
pub mode: TrackerMode,
180-
/// The url of the tracker API. For example: `http://localhost:1212`.
181-
pub api_url: String,
182-
/// The token used to authenticate with the tracker API.
183-
pub token: String,
184-
/// The amount of seconds the token is valid.
185-
pub token_valid_seconds: u64,
186-
}
187-
188-
impl Tracker {
189-
fn override_tracker_api_token(&mut self, tracker_api_token: &str) {
190-
self.token = tracker_api_token.to_string();
191-
}
192-
}
193-
194-
impl Default for Tracker {
195-
fn default() -> Self {
196-
Self {
197-
url: "udp://localhost:6969".to_string(),
198-
mode: TrackerMode::default(),
199-
api_url: "http://localhost:1212".to_string(),
200-
token: "MyAccessToken".to_string(),
201-
token_valid_seconds: 7_257_600,
202-
}
203-
}
204-
}
205-
206170
/// Port number representing that the OS will choose one randomly from the available ports.
207171
///
208172
/// It's the port number `0`
209173
pub const FREE_PORT: u16 = 0;
210174

211-
/// The the base URL for the API.
212-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
213-
pub struct Network {
214-
/// The port to listen on. Default to `3001`.
215-
pub port: u16,
216-
/// The base URL for the API. For example: `http://localhost`.
217-
/// If not set, the base URL will be inferred from the request.
218-
pub base_url: Option<String>,
219-
/// TSL configuration.
220-
pub tsl: Option<Tsl>,
221-
}
222-
223-
impl Default for Network {
224-
fn default() -> Self {
225-
Self {
226-
port: 3001,
227-
base_url: None,
228-
tsl: None,
229-
}
230-
}
231-
}
232-
233-
/// Whether the email is required on signup or not.
234-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
235-
pub enum EmailOnSignup {
236-
/// The email is required on signup.
237-
Required,
238-
/// The email is optional on signup.
239-
Optional,
240-
/// The email is not allowed on signup. It will only be ignored if provided.
241-
None, // code-review: rename to `Ignored`?
242-
}
243-
244-
impl Default for EmailOnSignup {
245-
fn default() -> Self {
246-
Self::Optional
247-
}
248-
}
249-
250-
/// Authentication options.
251-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
252-
pub struct Auth {
253-
/// Whether or not to require an email on signup.
254-
pub email_on_signup: EmailOnSignup,
255-
/// The minimum password length.
256-
pub min_password_length: usize,
257-
/// The maximum password length.
258-
pub max_password_length: usize,
259-
/// The secret key used to sign JWT tokens.
260-
pub secret_key: String,
261-
}
262-
263-
impl Default for Auth {
264-
fn default() -> Self {
265-
Self {
266-
email_on_signup: EmailOnSignup::default(),
267-
min_password_length: 6,
268-
max_password_length: 64,
269-
secret_key: "MaxVerstappenWC2021".to_string(),
270-
}
271-
}
272-
}
273-
274-
impl Auth {
275-
fn override_secret_key(&mut self, secret_key: &str) {
276-
self.secret_key = secret_key.to_string();
277-
}
278-
}
279-
280-
/// Database configuration.
281-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
282-
pub struct Database {
283-
/// The connection string for the database. For example: `sqlite://data.db?mode=rwc`.
284-
pub connect_url: String,
285-
}
286-
287-
impl Default for Database {
288-
fn default() -> Self {
289-
Self {
290-
connect_url: "sqlite://data.db?mode=rwc".to_string(),
291-
}
292-
}
293-
}
294-
295-
/// SMTP configuration.
296-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
297-
pub struct Mail {
298-
/// Whether or not to enable email verification on signup.
299-
pub email_verification_enabled: bool,
300-
/// The email address to send emails from.
301-
pub from: String,
302-
/// The email address to reply to.
303-
pub reply_to: String,
304-
/// The username to use for SMTP authentication.
305-
pub username: String,
306-
/// The password to use for SMTP authentication.
307-
pub password: String,
308-
/// The SMTP server to use.
309-
pub server: String,
310-
/// The SMTP port to use.
311-
pub port: u16,
312-
}
313-
314-
impl Default for Mail {
315-
fn default() -> Self {
316-
Self {
317-
email_verification_enabled: false,
318-
from: "[email protected]".to_string(),
319-
reply_to: "[email protected]".to_string(),
320-
username: String::default(),
321-
password: String::default(),
322-
server: String::default(),
323-
port: 25,
324-
}
325-
}
326-
}
327-
328-
/// Configuration for the image proxy cache.
329-
///
330-
/// Users have a cache quota per period. For example: 100MB per day.
331-
/// When users are navigating the site, they will be downloading images that are
332-
/// embedded in the torrent description. These images will be cached in the
333-
/// proxy. The proxy will not download new images if the user has reached the
334-
/// quota.
335-
#[allow(clippy::module_name_repetitions)]
336-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
337-
pub struct ImageCache {
338-
/// Maximum time in seconds to wait for downloading the image form the original source.
339-
pub max_request_timeout_ms: u64,
340-
/// Cache size in bytes.
341-
pub capacity: usize,
342-
/// Maximum size in bytes for a single image.
343-
pub entry_size_limit: usize,
344-
/// Users have a cache quota per period. For example: 100MB per day.
345-
/// This is the period in seconds (1 day in seconds).
346-
pub user_quota_period_seconds: u64,
347-
/// Users have a cache quota per period. For example: 100MB per day.
348-
/// This is the maximum size in bytes (100MB in bytes).
349-
pub user_quota_bytes: usize,
350-
}
351-
352-
/// Core configuration for the API
353-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
354-
pub struct Api {
355-
/// The default page size for torrent lists.
356-
pub default_torrent_page_size: u8,
357-
/// The maximum page size for torrent lists.
358-
pub max_torrent_page_size: u8,
359-
}
360-
361-
impl Default for Api {
362-
fn default() -> Self {
363-
Self {
364-
default_torrent_page_size: 10,
365-
max_torrent_page_size: 30,
366-
}
367-
}
368-
}
369-
370-
/// Configuration for the tracker statistics importer.
371-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
372-
pub struct TrackerStatisticsImporter {
373-
/// The interval in seconds to get statistics from the tracker.
374-
pub torrent_info_update_interval: u64,
375-
/// The port the Importer API is listening on. Default to `3002`.
376-
pub port: u16,
377-
}
378-
379-
impl Default for TrackerStatisticsImporter {
380-
fn default() -> Self {
381-
Self {
382-
torrent_info_update_interval: 3600,
383-
port: 3002,
384-
}
385-
}
386-
}
387-
388-
impl Default for ImageCache {
389-
fn default() -> Self {
390-
Self {
391-
max_request_timeout_ms: 1000,
392-
capacity: 128_000_000,
393-
entry_size_limit: 4_000_000,
394-
user_quota_period_seconds: 3600,
395-
user_quota_bytes: 64_000_000,
396-
}
397-
}
398-
}
399-
400175
#[serde_as]
401176
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
402177
pub struct Tsl {
@@ -422,49 +197,6 @@ impl Tsl {
422197
}
423198
}
424199

425-
/// The whole configuration for the index.
426-
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
427-
pub struct TorrustIndex {
428-
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
429-
/// `Debug` and `Trace`. Default is `Info`.
430-
pub log_level: Option<String>,
431-
/// The website customizable values.
432-
pub website: Website,
433-
/// The tracker configuration.
434-
pub tracker: Tracker,
435-
/// The network configuration.
436-
pub net: Network,
437-
/// The authentication configuration.
438-
pub auth: Auth,
439-
/// The database configuration.
440-
pub database: Database,
441-
/// The SMTP configuration.
442-
pub mail: Mail,
443-
/// The image proxy cache configuration.
444-
pub image_cache: ImageCache,
445-
/// The API configuration.
446-
pub api: Api,
447-
/// The tracker statistics importer job configuration.
448-
pub tracker_statistics_importer: TrackerStatisticsImporter,
449-
}
450-
451-
impl TorrustIndex {
452-
fn override_tracker_api_token(&mut self, tracker_api_token: &str) {
453-
self.tracker.override_tracker_api_token(tracker_api_token);
454-
}
455-
456-
fn override_auth_secret_key(&mut self, auth_secret_key: &str) {
457-
self.auth.override_secret_key(auth_secret_key);
458-
}
459-
460-
pub fn remove_secrets(&mut self) {
461-
"***".clone_into(&mut self.tracker.token);
462-
"***".clone_into(&mut self.database.connect_url);
463-
"***".clone_into(&mut self.mail.password);
464-
"***".clone_into(&mut self.auth.secret_key);
465-
}
466-
}
467-
468200
/// The configuration service.
469201
#[derive(Debug)]
470202
pub struct Configuration {

src/config/v1/api.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
/// Core configuration for the API
4+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5+
pub struct Api {
6+
/// The default page size for torrent lists.
7+
pub default_torrent_page_size: u8,
8+
/// The maximum page size for torrent lists.
9+
pub max_torrent_page_size: u8,
10+
}
11+
12+
impl Default for Api {
13+
fn default() -> Self {
14+
Self {
15+
default_torrent_page_size: 10,
16+
max_torrent_page_size: 30,
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)