Change settings to be done in the config directory
Signed-off-by: trivernis <trivernis@protonmail.com>pull/1/head
parent
033e78a680
commit
12f82e72f0
@ -1,4 +1,6 @@
|
||||
/target
|
||||
.idea
|
||||
*.env
|
||||
test
|
||||
test
|
||||
config
|
||||
nodes
|
||||
|
@ -0,0 +1,6 @@
|
||||
use vented::server::VentedServer;
|
||||
use crate::utils::result::SnekcloudResult;
|
||||
|
||||
pub trait Module {
|
||||
fn init(&mut self, server: &VentedServer) -> SnekcloudResult<()>;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
const VAR_KEY_FILE_STORAGE: &str = "SNEKCLOUD_NODES_DIR";
|
||||
const VAR_PRIVATE_KEY_PATH: &str = "SNEKCLOUD_PRIVATE_KEY";
|
||||
const VAR_LISTEN_ADDRESS: &str = "SNEKCLOUD_LISTEN_ADDRESS";
|
||||
const VAR_NODE_ID: &str = "SNEKCLOUD_NODE_ID";
|
||||
|
||||
pub fn get_key_file_storage() -> String {
|
||||
dotenv::var(VAR_KEY_FILE_STORAGE).unwrap_or("nodes".to_string())
|
||||
}
|
||||
|
||||
pub fn get_private_key_path() -> String {
|
||||
dotenv::var(VAR_PRIVATE_KEY_PATH).unwrap_or("node_key".to_string())
|
||||
}
|
||||
|
||||
pub fn get_env_node_id() -> Option<String> {
|
||||
dotenv::var(VAR_NODE_ID).ok()
|
||||
}
|
||||
|
||||
pub fn get_listen_address() -> String {
|
||||
dotenv::var(VAR_LISTEN_ADDRESS).unwrap_or("127.0.0.1:22222".to_string())
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
use crate::utils::result::{SnekcloudResult, SnekcloudError};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::utils::{get_node_id, write_toml_pretty};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use config::File;
|
||||
|
||||
|
||||
const CONFIG_DIR: &str = "config/";
|
||||
const DEFAULT_CONFIG: &str = "config/00_default.toml";
|
||||
const GLOB_CONFIG: &str = "config/*.toml";
|
||||
const ENV_PREFIX : &str = "SNEKCLOUD";
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Settings {
|
||||
pub listen_addresses: Vec<String>,
|
||||
pub node_id: String,
|
||||
pub private_key: PathBuf,
|
||||
pub node_data_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen_addresses: vec!["127.0.0.1:22222".to_string()],
|
||||
node_id: get_node_id(),
|
||||
private_key: PathBuf::from("node_key"),
|
||||
node_data_dir: PathBuf::from("nodes"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_settings() -> SnekcloudResult<Settings> {
|
||||
if !Path::new(CONFIG_DIR).exists() {
|
||||
fs::create_dir(CONFIG_DIR)?;
|
||||
}
|
||||
write_toml_pretty(&PathBuf::from(DEFAULT_CONFIG), &Settings::default())?;
|
||||
|
||||
let mut settings = config::Config::default();
|
||||
settings
|
||||
.merge(config::File::with_name(DEFAULT_CONFIG))?
|
||||
.merge(glob::glob(GLOB_CONFIG)?.map(|path| File::from(path.unwrap()))
|
||||
.collect::<Vec<_>>())?
|
||||
.merge(config::Environment::with_prefix(ENV_PREFIX))?;
|
||||
|
||||
|
||||
settings.try_into().map_err(SnekcloudError::from)
|
||||
}
|
Loading…
Reference in New Issue