]> Untitled Git - lemmy.git/blobdiff - server/src/settings.rs
Merge branch 'dev' into federation
[lemmy.git] / server / src / settings.rs
index 7d558c5e17579a32d539085d99043cb34a26c79e..8c3cd6a1251d035b7220a43f5e7440e266220767 100644 (file)
@@ -1,24 +1,37 @@
-extern crate lazy_static;
 use config::{Config, ConfigError, Environment, File};
+use failure::Error;
 use serde::Deserialize;
 use std::env;
+use std::fs;
 use std::net::IpAddr;
+use std::sync::RwLock;
 
 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
-static CONFIG_FILE_COSTUMIZED: &str = "config/custom.hjson";
+static CONFIG_FILE: &str = "config/config.hjson";
 
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
 pub struct Settings {
+  pub setup: Option<Setup>,
   pub database: Database,
   pub hostname: String,
   pub bind: IpAddr,
   pub port: u16,
   pub jwt_secret: String,
+  pub front_end_dir: String,
   pub rate_limit: RateLimitConfig,
   pub email: Option<EmailConfig>,
+  pub federation: Federation,
 }
 
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
+pub struct Setup {
+  pub admin_username: String,
+  pub admin_password: String,
+  pub admin_email: Option<String>,
+  pub site_name: String,
+}
+
+#[derive(Debug, Deserialize, Clone)]
 pub struct RateLimitConfig {
   pub message: i32,
   pub message_per_second: i32,
@@ -28,15 +41,16 @@ pub struct RateLimitConfig {
   pub register_per_second: i32,
 }
 
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
 pub struct EmailConfig {
   pub smtp_server: String,
-  pub smtp_login: String,
-  pub smtp_password: String,
+  pub smtp_login: Option<String>,
+  pub smtp_password: Option<String>,
   pub smtp_from_address: String,
+  pub use_tls: bool,
 }
 
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, Clone)]
 pub struct Database {
   pub user: String,
   pub password: String,
@@ -46,41 +60,45 @@ pub struct Database {
   pub pool_size: u32,
 }
 
+#[derive(Debug, Deserialize)]
+pub struct Federation {
+  pub enabled: bool,
+  pub followed_instances: String,
+  pub tls_enabled: bool,
+}
+
 lazy_static! {
-  static ref SETTINGS: Settings = {
-    return match Settings::init() {
-      Ok(c) => c,
-      Err(e) => panic!("{}", e),
-    };
-  };
+  static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
+    Ok(c) => c,
+    Err(e) => panic!("{}", e),
+  });
 }
 
 impl Settings {
-  
   /// Reads config from the files and environment.
   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
-  /// from CONFIG_FILE_COSTUMIZED (optional). Finally, values from the environment
-  /// (with prefix LEMMY) are added to the config.
+  /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
+  /// added to the config.
   fn init() -> Result<Self, ConfigError> {
     let mut s = Config::new();
 
-    // Start off by merging in the "default" configuration file
     s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
 
-    // TODO: we could also automatically load dev/prod configs based on environment
-    // https://github.com/mehcode/config-rs/blob/master/examples/hierarchical-env/src/settings.rs#L49
-    s.merge(File::with_name(CONFIG_FILE_COSTUMIZED).required(false))?;
+    s.merge(File::with_name(CONFIG_FILE).required(false))?;
 
     // Add in settings from the environment (with a prefix of LEMMY)
     // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
-    s.merge(Environment::with_prefix("LEMMY"))?;
+    // Note: we need to use double underscore here, because otherwise variables containing
+    //       underscore cant be set from environmnet.
+    // https://github.com/mehcode/config-rs/issues/73
+    s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
 
-    return s.try_into();
+    s.try_into()
   }
 
   /// Returns the config as a struct.
-  pub fn get() -> &'static Self {
-    &SETTINGS
+  pub fn get() -> Self {
+    SETTINGS.read().unwrap().to_owned()
   }
 
   /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
@@ -102,4 +120,22 @@ impl Settings {
   pub fn api_endpoint(&self) -> String {
     format!("{}/api/v1", self.hostname)
   }
+
+  pub fn read_config_file() -> Result<String, Error> {
+    Ok(fs::read_to_string(CONFIG_FILE)?)
+  }
+
+  pub fn save_config_file(data: &str) -> Result<String, Error> {
+    fs::write(CONFIG_FILE, data)?;
+
+    // Reload the new settings
+    // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
+    let mut new_settings = SETTINGS.write().unwrap();
+    *new_settings = match Settings::init() {
+      Ok(c) => c,
+      Err(e) => panic!("{}", e),
+    };
+
+    Self::read_config_file()
+  }
 }