]> Untitled Git - lemmy.git/blob - crates/utils/src/settings/mod.rs
abd6f733cde9a641a13569df57921c6bb89bb46c
[lemmy.git] / crates / utils / src / settings / mod.rs
1 use crate::{
2   location_info,
3   settings::structs::{
4     CaptchaConfig,
5     DatabaseConfig,
6     EmailConfig,
7     FederationConfig,
8     RateLimitConfig,
9     Settings,
10     SetupConfig,
11   },
12   LemmyError,
13 };
14 use anyhow::{anyhow, Context};
15 use deser_hjson::from_str;
16 use merge::Merge;
17 use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
18
19 pub(crate) mod defaults;
20 pub mod structs;
21
22 static CONFIG_FILE: &str = "config/config.hjson";
23
24 lazy_static! {
25   static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
26     Ok(c) => c,
27     Err(e) => panic!("{}", e),
28   });
29 }
30
31 impl Settings {
32   /// Reads config from the files and environment.
33   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
34   /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
35   /// added to the config.
36   ///
37   /// Note: The env var `LEMMY_DATABASE_URL` is parsed in
38   /// `lemmy_db_queries/src/lib.rs::get_database_url_from_env()`
39   fn init() -> Result<Self, LemmyError> {
40     // Read the config file
41     let mut custom_config = from_str::<Settings>(&Self::read_config_file()?)?;
42
43     // Merge with env vars
44     custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?);
45
46     // Merge with default
47     custom_config.merge(Settings::default());
48
49     if custom_config.hostname == Settings::default().hostname {
50       return Err(anyhow!("Hostname variable is not set!").into());
51     }
52
53     Ok(custom_config)
54   }
55
56   /// Returns the config as a struct.
57   pub fn get() -> Self {
58     SETTINGS.read().unwrap().to_owned()
59   }
60
61   pub fn get_database_url(&self) -> String {
62     let conf = self.database();
63     format!(
64       "postgres://{}:{}@{}:{}/{}",
65       conf.user, conf.password, conf.host, conf.port, conf.database,
66     )
67   }
68
69   pub fn get_config_location() -> String {
70     env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string())
71   }
72
73   pub fn read_config_file() -> Result<String, Error> {
74     fs::read_to_string(Self::get_config_location())
75   }
76
77   pub fn get_allowed_instances(&self) -> Option<Vec<String>> {
78     self.federation().allowed_instances
79   }
80
81   pub fn get_blocked_instances(&self) -> Option<Vec<String>> {
82     self.federation().blocked_instances
83   }
84
85   /// Returns either "http" or "https", depending on tls_enabled setting
86   pub fn get_protocol_string(&self) -> &'static str {
87     if let Some(tls_enabled) = self.tls_enabled {
88       if tls_enabled {
89         "https"
90       } else {
91         "http"
92       }
93     } else {
94       "http"
95     }
96   }
97
98   /// Returns something like `http://localhost` or `https://lemmy.ml`,
99   /// with the correct protocol and hostname.
100   pub fn get_protocol_and_hostname(&self) -> String {
101     format!("{}://{}", self.get_protocol_string(), self.hostname())
102   }
103
104   /// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
105   /// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
106   /// `lemmy-alpha` instead. It has no effect in production.
107   pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
108     Ok(
109       self
110         .hostname()
111         .split(':')
112         .collect::<Vec<&str>>()
113         .first()
114         .context(location_info!())?
115         .to_string(),
116     )
117   }
118
119   pub fn save_config_file(data: &str) -> Result<String, Error> {
120     fs::write(CONFIG_FILE, data)?;
121
122     // Reload the new settings
123     // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
124     let mut new_settings = SETTINGS.write().unwrap();
125     *new_settings = match Settings::init() {
126       Ok(c) => c,
127       Err(e) => panic!("{}", e),
128     };
129
130     Self::read_config_file()
131   }
132
133   pub fn database(&self) -> DatabaseConfig {
134     self.database.to_owned().unwrap_or_default()
135   }
136   pub fn hostname(&self) -> String {
137     self.hostname.to_owned().unwrap_or_default()
138   }
139   pub fn bind(&self) -> IpAddr {
140     self.bind.unwrap()
141   }
142   pub fn port(&self) -> u16 {
143     self.port.unwrap_or_default()
144   }
145   pub fn tls_enabled(&self) -> bool {
146     self.tls_enabled.unwrap_or_default()
147   }
148   pub fn jwt_secret(&self) -> String {
149     self.jwt_secret.to_owned().unwrap_or_default()
150   }
151   pub fn pictrs_url(&self) -> String {
152     self.pictrs_url.to_owned().unwrap_or_default()
153   }
154   pub fn iframely_url(&self) -> String {
155     self.iframely_url.to_owned().unwrap_or_default()
156   }
157   pub fn rate_limit(&self) -> RateLimitConfig {
158     self.rate_limit.to_owned().unwrap_or_default()
159   }
160   pub fn federation(&self) -> FederationConfig {
161     self.federation.to_owned().unwrap_or_default()
162   }
163   pub fn captcha(&self) -> CaptchaConfig {
164     self.captcha.to_owned().unwrap_or_default()
165   }
166   pub fn email(&self) -> Option<EmailConfig> {
167     self.email.to_owned()
168   }
169   pub fn setup(&self) -> Option<SetupConfig> {
170     self.setup.to_owned()
171   }
172 }