torrust_index/web/api/client/v1/contexts/settings/
mod.rs

1pub mod responses;
2
3use std::net::SocketAddr;
4
5use serde::{Deserialize, Serialize};
6use url::Url;
7
8use crate::config::v2::tracker::ApiToken;
9use crate::config::{
10    Api as DomainApi, Auth as DomainAuth, Credentials as DomainCredentials, Database as DomainDatabase,
11    ImageCache as DomainImageCache, Mail as DomainMail, Network as DomainNetwork,
12    PasswordConstraints as DomainPasswordConstraints, Settings as DomainSettings, Smtp as DomainSmtp, Tracker as DomainTracker,
13    TrackerStatisticsImporter as DomainTrackerStatisticsImporter, Website as DomainWebsite,
14};
15
16#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
17pub struct Settings {
18    pub website: Website,
19    pub tracker: Tracker,
20    pub net: Network,
21    pub auth: Auth,
22    pub database: Database,
23    pub mail: Mail,
24    pub image_cache: ImageCache,
25    pub api: Api,
26    pub tracker_statistics_importer: TrackerStatisticsImporter,
27}
28
29#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
30pub struct Website {
31    pub name: String,
32}
33
34#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
35pub struct Tracker {
36    pub url: Url,
37    pub listed: bool,
38    pub private: bool,
39    pub api_url: Url,
40    pub token: ApiToken,
41    pub token_valid_seconds: u64,
42}
43
44#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
45pub struct Network {
46    pub base_url: Option<String>,
47    pub bind_address: SocketAddr,
48}
49
50#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
51pub struct Auth {
52    pub user_claim_token_pepper: String,
53    pub password_constraints: PasswordConstraints,
54}
55
56#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
57pub struct PasswordConstraints {
58    pub min_password_length: usize,
59    pub max_password_length: usize,
60}
61
62#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
63pub struct Database {
64    pub connect_url: String,
65}
66
67#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
68pub struct Mail {
69    pub from: String,
70    pub reply_to: String,
71    pub smtp: Smtp,
72}
73
74#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
75pub struct Smtp {
76    pub server: String,
77    pub port: u16,
78    pub credentials: Credentials,
79}
80
81#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
82pub struct Credentials {
83    pub username: String,
84    pub password: String,
85}
86
87#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
88pub struct ImageCache {
89    pub max_request_timeout_ms: u64,
90    pub capacity: usize,
91    pub entry_size_limit: usize,
92    pub user_quota_period_seconds: u64,
93    pub user_quota_bytes: usize,
94}
95
96#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
97pub struct Api {
98    pub default_torrent_page_size: u8,
99    pub max_torrent_page_size: u8,
100}
101
102#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
103pub struct TrackerStatisticsImporter {
104    pub torrent_info_update_interval: u64,
105    port: u16,
106}
107
108impl From<DomainSettings> for Settings {
109    fn from(settings: DomainSettings) -> Self {
110        Settings {
111            website: Website::from(settings.website),
112            tracker: Tracker::from(settings.tracker),
113            net: Network::from(settings.net),
114            auth: Auth::from(settings.auth),
115            database: Database::from(settings.database),
116            mail: Mail::from(settings.mail),
117            image_cache: ImageCache::from(settings.image_cache),
118            api: Api::from(settings.api),
119            tracker_statistics_importer: TrackerStatisticsImporter::from(settings.tracker_statistics_importer),
120        }
121    }
122}
123
124impl From<DomainWebsite> for Website {
125    fn from(website: DomainWebsite) -> Self {
126        Self { name: website.name }
127    }
128}
129
130impl From<DomainTracker> for Tracker {
131    fn from(tracker: DomainTracker) -> Self {
132        Self {
133            url: tracker.url,
134            listed: tracker.listed,
135            private: tracker.private,
136            api_url: tracker.api_url,
137            token: tracker.token,
138            token_valid_seconds: tracker.token_valid_seconds,
139        }
140    }
141}
142
143impl From<DomainNetwork> for Network {
144    fn from(net: DomainNetwork) -> Self {
145        Self {
146            base_url: net.base_url.map(|url_without_port| url_without_port.to_string()),
147            bind_address: net.bind_address,
148        }
149    }
150}
151
152impl From<DomainAuth> for Auth {
153    fn from(auth: DomainAuth) -> Self {
154        Self {
155            user_claim_token_pepper: auth.user_claim_token_pepper.to_string(),
156            password_constraints: auth.password_constraints.into(),
157        }
158    }
159}
160
161impl From<DomainPasswordConstraints> for PasswordConstraints {
162    fn from(password_constraints: DomainPasswordConstraints) -> Self {
163        Self {
164            min_password_length: password_constraints.min_password_length,
165            max_password_length: password_constraints.max_password_length,
166        }
167    }
168}
169
170impl From<DomainDatabase> for Database {
171    fn from(database: DomainDatabase) -> Self {
172        Self {
173            connect_url: database.connect_url.to_string(),
174        }
175    }
176}
177
178impl From<DomainMail> for Mail {
179    fn from(mail: DomainMail) -> Self {
180        Self {
181            from: mail.from.to_string(),
182            reply_to: mail.reply_to.to_string(),
183            smtp: Smtp::from(mail.smtp),
184        }
185    }
186}
187
188impl From<DomainSmtp> for Smtp {
189    fn from(smtp: DomainSmtp) -> Self {
190        Self {
191            server: smtp.server,
192            port: smtp.port,
193            credentials: Credentials::from(smtp.credentials),
194        }
195    }
196}
197
198impl From<DomainCredentials> for Credentials {
199    fn from(credentials: DomainCredentials) -> Self {
200        Self {
201            username: credentials.username,
202            password: credentials.password,
203        }
204    }
205}
206
207impl From<DomainImageCache> for ImageCache {
208    fn from(image_cache: DomainImageCache) -> Self {
209        Self {
210            max_request_timeout_ms: image_cache.max_request_timeout_ms,
211            capacity: image_cache.capacity,
212            entry_size_limit: image_cache.entry_size_limit,
213            user_quota_period_seconds: image_cache.user_quota_period_seconds,
214            user_quota_bytes: image_cache.user_quota_bytes,
215        }
216    }
217}
218
219impl From<DomainApi> for Api {
220    fn from(api: DomainApi) -> Self {
221        Self {
222            default_torrent_page_size: api.default_torrent_page_size,
223            max_torrent_page_size: api.max_torrent_page_size,
224        }
225    }
226}
227
228impl From<DomainTrackerStatisticsImporter> for TrackerStatisticsImporter {
229    fn from(tracker_statistics_importer: DomainTrackerStatisticsImporter) -> Self {
230        Self {
231            torrent_info_update_interval: tracker_statistics_importer.torrent_info_update_interval,
232            port: tracker_statistics_importer.port,
233        }
234    }
235}