Get CONFIG_PATH from env_variable
This commit is contained in:
parent
e2ac5de6e4
commit
29804b0dce
|
@ -2430,6 +2430,7 @@ dependencies = [
|
|||
"jsonrpc-client-core",
|
||||
"jsonrpc-client-http",
|
||||
"jsonrpc-core 8.0.1",
|
||||
"lazy_static",
|
||||
"log 0.4.16",
|
||||
"nanorand",
|
||||
"regex",
|
||||
|
|
|
@ -21,3 +21,4 @@ serde_json = "1.0"
|
|||
serde_yaml = "0.8"
|
||||
toml = "0.5"
|
||||
sha3 = "0.10"
|
||||
lazy_static = "1.4.0"
|
||||
|
|
|
@ -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<PeachConfig, PeachError> {
|
||||
// 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<PeachConfig, Peach
|
|||
}
|
||||
|
||||
pub fn load_peach_config() -> Result<PeachConfig, PeachError> {
|
||||
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<PeachConfig, PeachError> {
|
|||
}
|
||||
// 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)?
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue