diff --git a/Cargo.lock b/Cargo.lock index b56d9dc..989b105 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2430,6 +2430,7 @@ dependencies = [ "jsonrpc-client-core", "jsonrpc-client-http", "jsonrpc-core 8.0.1", + "lazy_static", "log 0.4.16", "nanorand", "regex", diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index 9d53e7f..58e5789 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -21,3 +21,4 @@ serde_json = "1.0" serde_yaml = "0.8" toml = "0.5" sha3 = "0.10" +lazy_static = "1.4.0" diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index e29f8d6..a11c23c 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -5,19 +5,34 @@ //! //! The configuration file is located at: "/var/lib/peachcloud/config.yml" -use std::fs; +use std::{env, fs}; +use lazy_static::lazy_static; use fslock::LockFile; use log::debug; use serde::{Deserialize, Serialize}; use crate::error::PeachError; -// main configuration file -pub const YAML_PATH: &str = "/var/lib/peachcloud/config.yml"; +// load path to main configuration file +// from PEACH_CONFIG_PATH if that environment variable is set +// or using the default value if not set +pub const DEFAULT_YAML_PATH: &str = "/var/lib/peachcloud/config.yml"; +lazy_static! { + static ref CONFIG_PATH: String = { + if let Ok(val) = env::var("PEACH_CONFIG_PATH") { + val + } + else { + DEFAULT_YAML_PATH.to_string() + } + }; + // the lock file path is the config file path + ".lock" + static ref LOCK_FILE_PATH: String = format!("{}.lock", *CONFIG_PATH); +} // lock file (used to avoid race conditions during config reading & writing) -pub const LOCK_FILE_PATH: &str = "/var/lib/peachcloud/config.lock"; + // default values pub const DEFAULT_DYN_SERVER_ADDRESS: &str = "http://dynserver.dyn.peachcloud.org"; @@ -53,14 +68,14 @@ pub struct PeachConfig { // helper functions for serializing and deserializing PeachConfig from disc pub fn save_peach_config(peach_config: PeachConfig) -> Result { // use a file lock to avoid race conditions while saving config - let mut lock = LockFile::open(LOCK_FILE_PATH)?; + let mut lock = LockFile::open(&*LOCK_FILE_PATH)?; lock.lock()?; let yaml_str = serde_yaml::to_string(&peach_config)?; - fs::write(YAML_PATH, yaml_str).map_err(|source| PeachError::Write { + fs::write((*CONFIG_PATH).to_string(), yaml_str).map_err(|source| PeachError::Write { source, - path: YAML_PATH.to_string(), + path: (*CONFIG_PATH).to_string(), })?; // unlock file lock @@ -71,10 +86,10 @@ pub fn save_peach_config(peach_config: PeachConfig) -> Result Result { - let peach_config_exists = std::path::Path::new(YAML_PATH).exists(); + let peach_config_exists = std::path::Path::new(&*CONFIG_PATH).exists(); let peach_config: PeachConfig = if !peach_config_exists { - debug!("Loading peach config: {} does not exist", YAML_PATH); + debug!("Loading peach config: {} does not exist", *CONFIG_PATH); PeachConfig { external_domain: "".to_string(), dyn_domain: "".to_string(), @@ -91,10 +106,10 @@ pub fn load_peach_config() -> Result { } // otherwise we load peach config from disk else { - debug!("Loading peach config: {} exists", YAML_PATH); - let contents = fs::read_to_string(YAML_PATH).map_err(|source| PeachError::Read { + debug!("Loading peach config: {} exists", *CONFIG_PATH); + let contents = fs::read_to_string((*CONFIG_PATH).to_string()).map_err(|source| PeachError::Read { source, - path: YAML_PATH.to_string(), + path: (*CONFIG_PATH).to_string(), })?; serde_yaml::from_str(&contents)? };