-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
338 lines (294 loc) · 10 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use std::path::PathBuf;
use anyhow::Result;
use schematic::{derive_enum, validate::url_secure, Config, ConfigEnum, ConfigLoader};
use serde::{Deserialize, Serialize};
use crate::graphql::PROJECT_NAME;
fn default_anilist_url(_ctx: &()) -> Option<String> {
Some("https://graphql.anilist.co".to_owned())
}
fn default_tmdb_url(_ctx: &()) -> Option<String> {
Some("https://api.themoviedb.org/3/".to_owned())
}
fn default_tmdb_access_token(_ctx: &()) -> Option<String> {
Some("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI4ZGVlOTZjMjc0OGVhY2U0NzU2MGJkMWU4YzE5NTljMCIsInN1YiI6IjY0NDRiYmE4MmM2YjdiMDRiZTdlZDJmNSIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.ZZZNJMXStvAOPJlT0hOBVPSTppFAK3mcUpmbJsExIq4".to_owned())
}
/// Determine whether a feature is enabled
pub trait IsFeatureEnabled {
fn is_enabled(&self) -> bool {
true
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "ANIME_ANILIST_")]
pub struct AnimeAnilistConfig {
#[setting(validate = url_secure, default = default_anilist_url)]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct AnimeConfig {
#[setting(nested)]
pub anilist: AnimeAnilistConfig,
}
impl IsFeatureEnabled for AnimeConfig {}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "AUDIO_BOOKS_AUDIBLE_")]
pub struct AudibleConfig {
#[setting(validate = url_secure, default = "https://api.audible.com/1.0/catalog/products/")]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct AudioBookConfig {
#[setting(nested)]
pub audible: AudibleConfig,
}
impl IsFeatureEnabled for AudioBookConfig {}
derive_enum!(
#[derive(ConfigEnum, Default)]
pub enum OpenlibraryCoverImageSize {
#[serde(rename = "S")]
Small,
#[default]
#[serde(rename = "M")]
Medium,
#[serde(rename = "L")]
Large,
}
);
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "BOOKS_OPENLIBRARY_")]
pub struct OpenlibraryConfig {
pub cover_image_size: OpenlibraryCoverImageSize,
#[setting(validate = url_secure, default = "https://covers.openlibrary.org/b")]
pub cover_image_url: String,
#[setting(validate = url_secure, default = "https://openlibrary.org")]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct BookConfig {
#[setting(nested)]
pub openlibrary: OpenlibraryConfig,
}
impl IsFeatureEnabled for BookConfig {}
#[derive(Debug, Serialize, Deserialize, Clone, Config, PartialEq, Eq)]
#[config(rename_all = "snake_case", env_prefix = "DATABASE_")]
pub struct DatabaseConfig {
#[setting(default = format!("/data/{}-scdb.db", PROJECT_NAME))]
pub scdb_url: String,
#[setting(default = format!("sqlite:/data/{}.db?mode=rwc", PROJECT_NAME))]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "MOVIES_TMDB_")]
pub struct MoviesTmdbConfig {
#[setting(default = default_tmdb_access_token)]
pub access_token: String,
#[setting(validate = url_secure, default = default_tmdb_url)]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct MovieConfig {
#[setting(nested)]
pub tmdb: MoviesTmdbConfig,
}
impl IsFeatureEnabled for MovieConfig {}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "MANGA_ANILIST_")]
pub struct MangaAnilistConfig {
#[setting(validate = url_secure, default = default_anilist_url)]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct MangaConfig {
#[setting(nested)]
pub anilist: MangaAnilistConfig,
}
impl IsFeatureEnabled for MangaConfig {}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "PODCASTS_LISTENNOTES_")]
pub struct ListenNotesConfig {
pub api_token: String,
#[setting(validate = url_secure, default = "https://listen-api.listennotes.com/api/v2/")]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct PodcastConfig {
#[setting(nested)]
pub listennotes: ListenNotesConfig,
}
impl IsFeatureEnabled for PodcastConfig {
fn is_enabled(&self) -> bool {
let mut enabled = false;
if !self.listennotes.api_token.is_empty() {
enabled = true;
}
enabled
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "MOVIES_TMDB_")]
pub struct ShowsTmdbConfig {
#[setting(default = default_tmdb_access_token)]
pub access_token: String,
#[setting(validate = url_secure, default = default_tmdb_url)]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct ShowConfig {
#[setting(nested)]
pub tmdb: ShowsTmdbConfig,
}
impl IsFeatureEnabled for ShowConfig {}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "VIDEO_GAMES_TWITCH_")]
pub struct TwitchConfig {
// Endpoint used to get access tokens which will be used by IGDB
#[setting(validate = url_secure, default = "https://id.twitch.tv/oauth2/token")]
pub access_token_url: String,
pub client_id: String,
pub client_secret: String,
}
derive_enum!(
#[derive(ConfigEnum, Default)]
pub enum IgdbImageSize {
#[default]
#[serde(rename = "t_original")]
Original,
}
);
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "VIDEO_GAMES_IGDB_")]
pub struct IgdbConfig {
pub image_size: IgdbImageSize,
#[setting(validate = url_secure, default = "https://images.igdb.com/igdb/image/upload/")]
pub image_url: String,
#[setting(validate = url_secure, default = "https://api.igdb.com/v4/")]
pub url: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
pub struct VideoGameConfig {
#[setting(nested)]
pub igdb: IgdbConfig,
#[setting(nested)]
pub twitch: TwitchConfig,
}
impl IsFeatureEnabled for VideoGameConfig {
fn is_enabled(&self) -> bool {
let mut enabled = false;
if !self.twitch.client_id.is_empty() && !self.twitch.client_secret.is_empty() {
enabled = true;
}
enabled
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "FILE_STORAGE_")]
pub struct FileStorageConfig {
pub s3_access_key_id: String,
pub s3_bucket_name: String,
#[setting(default = "us-east-1")]
pub s3_region: String,
pub s3_secret_access_key: String,
pub s3_url: String,
}
impl IsFeatureEnabled for FileStorageConfig {
fn is_enabled(&self) -> bool {
let mut enabled = false;
if !self.s3_access_key_id.is_empty()
&& !self.s3_bucket_name.is_empty()
&& !self.s3_secret_access_key.is_empty()
{
enabled = true;
}
enabled
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "SCHEDULER_")]
pub struct SchedulerConfig {
#[setting(default = "sqlite::memory:")]
pub database_url: String,
#[setting(default = 5)]
pub rate_limit_num: i32,
#[setting(default = 12)]
pub user_cleanup_every: i32,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "USERS_")]
pub struct UsersConfig {
#[setting(default = true)]
pub allow_changing_username: bool,
#[setting(default = 90)]
pub token_valid_for_days: i32,
#[setting(default = true)]
pub allow_registration: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case", env_prefix = "WEB_")]
pub struct WebConfig {
#[setting(default = vec![], parse_env = schematic::env::split_comma)]
pub cors_origins: Vec<String>,
pub insecure_cookie: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone, Config)]
#[config(rename_all = "snake_case")]
pub struct AppConfig {
#[setting(nested)]
pub anime: AnimeConfig,
#[setting(nested)]
pub audio_books: AudioBookConfig,
#[setting(nested)]
pub books: BookConfig,
#[setting(nested)]
pub database: DatabaseConfig,
#[setting(nested)]
pub file_storage: FileStorageConfig,
#[setting(nested)]
pub manga: MangaConfig,
#[setting(nested)]
pub movies: MovieConfig,
#[setting(nested)]
pub podcasts: PodcastConfig,
#[setting(nested)]
pub scheduler: SchedulerConfig,
#[setting(nested)]
pub shows: ShowConfig,
#[setting(nested)]
pub users: UsersConfig,
#[setting(nested)]
pub video_games: VideoGameConfig,
#[setting(nested)]
pub web: WebConfig,
}
impl AppConfig {
// TODO: Denote masked values via attribute
pub fn masked_value(&self) -> Self {
let gt = || "****".to_owned();
let mut cl = self.clone();
cl.database.url = gt();
cl.database.scdb_url = gt();
cl.file_storage.s3_region = gt();
cl.file_storage.s3_bucket_name = gt();
cl.file_storage.s3_access_key_id = gt();
cl.file_storage.s3_secret_access_key = gt();
cl.file_storage.s3_url = gt();
cl.movies.tmdb.access_token = gt();
cl.podcasts.listennotes.api_token = gt();
cl.shows.tmdb.access_token = gt();
cl.scheduler.database_url = gt();
cl.video_games.twitch.client_id = gt();
cl.video_games.twitch.client_secret = gt();
cl.web.cors_origins = vec![gt()];
cl
}
}
pub fn get_app_config() -> Result<AppConfig> {
let config = "config";
let app = PROJECT_NAME;
let path = PathBuf::from(config);
let result = ConfigLoader::<AppConfig>::new()
.file_optional(path.join(format!("{app}.json")))?
.file_optional(path.join(format!("{app}.toml")))?
.file_optional(path.join(format!("{app}.yaml")))?
.load()?;
Ok(result.config)
}