]> Untitled Git - lemmy.git/blob - server/src/settings.rs
Initial post-listing community non-local.
[lemmy.git] / server / src / settings.rs
1 use config::{Config, ConfigError, Environment, File};
2 use serde::Deserialize;
3 use std::env;
4 use std::net::IpAddr;
5
6 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
7 static CONFIG_FILE: &str = "config/config.hjson";
8
9 #[derive(Debug, Deserialize)]
10 pub struct Settings {
11   pub setup: Option<Setup>,
12   pub database: Database,
13   pub hostname: String,
14   pub bind: IpAddr,
15   pub port: u16,
16   pub jwt_secret: String,
17   pub front_end_dir: String,
18   pub rate_limit: RateLimitConfig,
19   pub email: Option<EmailConfig>,
20   pub federation: Federation,
21 }
22
23 #[derive(Debug, Deserialize)]
24 pub struct Setup {
25   pub admin_username: String,
26   pub admin_password: String,
27   pub admin_email: Option<String>,
28   pub site_name: String,
29 }
30
31 #[derive(Debug, Deserialize)]
32 pub struct RateLimitConfig {
33   pub message: i32,
34   pub message_per_second: i32,
35   pub post: i32,
36   pub post_per_second: i32,
37   pub register: i32,
38   pub register_per_second: i32,
39 }
40
41 #[derive(Debug, Deserialize)]
42 pub struct EmailConfig {
43   pub smtp_server: String,
44   pub smtp_login: Option<String>,
45   pub smtp_password: Option<String>,
46   pub smtp_from_address: String,
47   pub use_tls: bool,
48 }
49
50 #[derive(Debug, Deserialize)]
51 pub struct Database {
52   pub user: String,
53   pub password: String,
54   pub host: String,
55   pub port: i32,
56   pub database: String,
57   pub pool_size: u32,
58 }
59
60 #[derive(Debug, Deserialize)]
61 pub struct Federation {
62   pub enabled: bool,
63   pub followed_instances: String,
64   pub tls_enabled: bool,
65 }
66
67 lazy_static! {
68   static ref SETTINGS: Settings = {
69     match Settings::init() {
70       Ok(c) => c,
71       Err(e) => panic!("{}", e),
72     }
73   };
74 }
75
76 impl Settings {
77   /// Reads config from the files and environment.
78   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
79   /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
80   /// added to the config.
81   fn init() -> Result<Self, ConfigError> {
82     let mut s = Config::new();
83
84     s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
85
86     s.merge(File::with_name(CONFIG_FILE).required(false))?;
87
88     // Add in settings from the environment (with a prefix of LEMMY)
89     // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
90     // Note: we need to use double underscore here, because otherwise variables containing
91     //       underscore cant be set from environmnet.
92     // https://github.com/mehcode/config-rs/issues/73
93     s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
94
95     s.try_into()
96   }
97
98   /// Returns the config as a struct.
99   pub fn get() -> &'static Self {
100     &SETTINGS
101   }
102
103   /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
104   /// otherwise the connection url is generated from the config.
105   pub fn get_database_url(&self) -> String {
106     match env::var("LEMMY_DATABASE_URL") {
107       Ok(url) => url,
108       Err(_) => format!(
109         "postgres://{}:{}@{}:{}/{}",
110         self.database.user,
111         self.database.password,
112         self.database.host,
113         self.database.port,
114         self.database.database
115       ),
116     }
117   }
118
119   pub fn api_endpoint(&self) -> String {
120     format!("{}/api/v1", self.hostname)
121   }
122 }