diff options
author | Dessalines <dessalines@users.noreply.github.com> | 2020-07-13 11:33:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-13 11:33:48 -0400 |
commit | e5d3e3a9c3e3257aaa5b1febd2893aa169c6c818 (patch) | |
tree | 248bd1bd7226a76b53ac5c9480ada6c7dbd15fe9 /server/lemmy_utils/src/settings.rs | |
parent | ea0881f87e4712810b0c1c7a6e510ba27b118577 (diff) |
Adding a configurable config location through an env var. (#960)
* Adding a configurable config location through an env var.
- Its `LEMMY_CONFIG_LOCATION`
- Fixes #764
* Using a static for CONFIG_FILE again.
* Make clippy happy
Diffstat (limited to 'server/lemmy_utils/src/settings.rs')
-rw-r--r-- | server/lemmy_utils/src/settings.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/server/lemmy_utils/src/settings.rs b/server/lemmy_utils/src/settings.rs index 2ce33f58..0607974f 100644 --- a/server/lemmy_utils/src/settings.rs +++ b/server/lemmy_utils/src/settings.rs @@ -1,6 +1,7 @@ use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::{fs, io::Error, net::IpAddr, sync::RwLock}; +use std::env; static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson"; static CONFIG_FILE: &str = "config/config.hjson"; @@ -83,7 +84,7 @@ impl Settings { s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?; - s.merge(File::with_name(CONFIG_FILE).required(false))?; + s.merge(File::with_name(&Self::get_config_location()).required(false))?; // Add in settings from the environment (with a prefix of LEMMY) // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key @@ -115,12 +116,16 @@ impl Settings { format!("{}/api/v1", self.hostname) } + pub fn get_config_location() -> String { + env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string()) + } + pub fn read_config_file() -> Result<String, Error> { - fs::read_to_string(CONFIG_FILE) + fs::read_to_string(Self::get_config_location()) } pub fn save_config_file(data: &str) -> Result<String, Error> { - fs::write(CONFIG_FILE, data)?; + fs::write(Self::get_config_location(), data)?; // Reload the new settings // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804 |