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
This commit is contained in:
Dessalines 2020-07-13 11:33:48 -04:00 committed by GitHub
parent ea0881f87e
commit e5d3e3a9c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -5,6 +5,8 @@ The configuration is based on the file
This file also contains documentation for all the available options. To override the defaults, you This file also contains documentation for all the available options. To override the defaults, you
can copy the options you want to change into your local `config.hjson` file. can copy the options you want to change into your local `config.hjson` file.
To use a different `config.hjson` location than the current directory, set the environment variable `LEMMY_CONFIG_LOCATION`.
Additionally, you can override any config files with environment variables. These have the same Additionally, you can override any config files with environment variables. These have the same
name as the config options, and are prefixed with `LEMMY_`. For example, you can override the name as the config options, and are prefixed with `LEMMY_`. For example, you can override the
`database.password` with `LEMMY_DATABASE__POOL_SIZE=10`. `database.password` with `LEMMY_DATABASE__POOL_SIZE=10`.

View file

@ -1,6 +1,7 @@
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
use serde::Deserialize; use serde::Deserialize;
use std::{fs, io::Error, net::IpAddr, sync::RwLock}; use std::{fs, io::Error, net::IpAddr, sync::RwLock};
use std::env;
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson"; static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
static CONFIG_FILE: &str = "config/config.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_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) // Add in settings from the environment (with a prefix of LEMMY)
// Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
@ -115,12 +116,16 @@ impl Settings {
format!("{}/api/v1", self.hostname) 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> { 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> { 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 // Reload the new settings
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804 // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804