14 use anyhow::{anyhow, Context};
15 use deser_hjson::from_str;
17 use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
19 pub(crate) mod defaults;
22 static CONFIG_FILE: &str = "config/config.hjson";
25 static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
27 Err(e) => panic!("{}", e),
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.
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()?)?;
43 // Merge with env vars
44 custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?);
47 custom_config.merge(Settings::default());
49 if custom_config.hostname == Settings::default().hostname {
50 return Err(anyhow!("Hostname variable is not set!").into());
56 /// Returns the config as a struct.
57 pub fn get() -> Self {
58 SETTINGS.read().expect("read config").to_owned()
61 pub fn get_database_url(&self) -> String {
62 let conf = self.database();
64 "postgres://{}:{}@{}:{}/{}",
65 conf.user, conf.password, conf.host, conf.port, conf.database,
69 pub fn get_config_location() -> String {
70 env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string())
73 pub fn read_config_file() -> Result<String, Error> {
74 fs::read_to_string(Self::get_config_location())
77 pub fn get_allowed_instances(&self) -> Option<Vec<String>> {
78 self.federation().allowed_instances
81 pub fn get_blocked_instances(&self) -> Option<Vec<String>> {
82 self.federation().blocked_instances
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 {
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())
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> {
112 .collect::<Vec<&str>>()
114 .context(location_info!())?
119 pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
120 fs::write(CONFIG_FILE, data)?;
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().expect("write config");
125 *new_settings = match Settings::init() {
127 Err(e) => panic!("{}", e),
130 Ok(Self::read_config_file()?)
133 pub fn database(&self) -> DatabaseConfig {
134 self.database.to_owned().unwrap_or_default()
136 pub fn hostname(&self) -> String {
137 self.hostname.to_owned().unwrap_or_default()
139 pub fn bind(&self) -> IpAddr {
140 self.bind.expect("return bind address")
142 pub fn port(&self) -> u16 {
143 self.port.unwrap_or_default()
145 pub fn tls_enabled(&self) -> bool {
146 self.tls_enabled.unwrap_or_default()
148 pub fn jwt_secret(&self) -> String {
149 self.jwt_secret.to_owned().unwrap_or_default()
151 pub fn pictrs_url(&self) -> String {
152 self.pictrs_url.to_owned().unwrap_or_default()
154 pub fn iframely_url(&self) -> String {
155 self.iframely_url.to_owned().unwrap_or_default()
157 pub fn rate_limit(&self) -> RateLimitConfig {
158 self.rate_limit.to_owned().unwrap_or_default()
160 pub fn federation(&self) -> FederationConfig {
161 self.federation.to_owned().unwrap_or_default()
163 pub fn captcha(&self) -> CaptchaConfig {
164 self.captcha.to_owned().unwrap_or_default()
166 pub fn email(&self) -> Option<EmailConfig> {
167 self.email.to_owned()
169 pub fn setup(&self) -> Option<SetupConfig> {
170 self.setup.to_owned()