]> Untitled Git - lemmy.git/blob - crates/utils/src/settings/structs.rs
1046bd147d6987b13bc8ebea543630cf70e52ffd
[lemmy.git] / crates / utils / src / settings / structs.rs
1 use merge::Merge;
2 use serde::Deserialize;
3 use std::net::IpAddr;
4
5 #[derive(Debug, Deserialize, Clone, Merge)]
6 pub struct Settings {
7   pub(crate) database: Option<DatabaseConfig>,
8   pub(crate) rate_limit: Option<RateLimitConfig>,
9   pub(crate) federation: Option<FederationConfig>,
10   pub(crate) hostname: Option<String>,
11   pub(crate) bind: Option<IpAddr>,
12   pub(crate) port: Option<u16>,
13   pub(crate) tls_enabled: Option<bool>,
14   pub(crate) jwt_secret: Option<String>,
15   pub(crate) pictrs_url: Option<String>,
16   pub(crate) iframely_url: Option<String>,
17   pub(crate) captcha: Option<CaptchaConfig>,
18   pub(crate) email: Option<EmailConfig>,
19   pub(crate) setup: Option<SetupConfig>,
20   pub(crate) additional_slurs: Option<String>,
21   pub(crate) actor_name_max_length: Option<usize>,
22 }
23
24 #[derive(Debug, Deserialize, Clone)]
25 pub struct CaptchaConfig {
26   pub enabled: bool,
27   pub difficulty: String,
28 }
29
30 #[derive(Debug, Deserialize, Clone)]
31 pub struct DatabaseConfig {
32   pub(super) user: Option<String>,
33   pub password: String,
34   pub host: String,
35   pub(super) port: Option<i32>,
36   pub(super) database: Option<String>,
37   pub(super) pool_size: Option<u32>,
38 }
39
40 impl DatabaseConfig {
41   pub fn user(&self) -> String {
42     self
43       .user
44       .to_owned()
45       .unwrap_or_else(|| DatabaseConfig::default().user.expect("missing user"))
46   }
47   pub fn port(&self) -> i32 {
48     self
49       .port
50       .unwrap_or_else(|| DatabaseConfig::default().port.expect("missing port"))
51   }
52   pub fn database(&self) -> String {
53     self.database.to_owned().unwrap_or_else(|| {
54       DatabaseConfig::default()
55         .database
56         .expect("missing database")
57     })
58   }
59   pub fn pool_size(&self) -> u32 {
60     self.pool_size.unwrap_or_else(|| {
61       DatabaseConfig::default()
62         .pool_size
63         .expect("missing pool_size")
64     })
65   }
66 }
67
68 #[derive(Debug, Deserialize, Clone)]
69 pub struct EmailConfig {
70   pub smtp_server: String,
71   pub smtp_login: Option<String>,
72   pub smtp_password: Option<String>,
73   pub smtp_from_address: String,
74   pub use_tls: bool,
75 }
76
77 #[derive(Debug, Deserialize, Clone)]
78 pub struct FederationConfig {
79   pub enabled: bool,
80   pub allowed_instances: Option<Vec<String>>,
81   pub blocked_instances: Option<Vec<String>>,
82   pub strict_allowlist: Option<bool>,
83 }
84
85 #[derive(Debug, Deserialize, Clone)]
86 pub struct RateLimitConfig {
87   pub message: i32,
88   pub message_per_second: i32,
89   pub post: i32,
90   pub post_per_second: i32,
91   pub register: i32,
92   pub register_per_second: i32,
93   pub image: i32,
94   pub image_per_second: i32,
95 }
96
97 #[derive(Debug, Deserialize, Clone)]
98 pub struct SetupConfig {
99   pub admin_username: String,
100   pub admin_password: String,
101   pub admin_email: Option<String>,
102   pub site_name: String,
103 }