mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
use generic struct for loading configurations
This commit is contained in:
@@ -5,7 +5,7 @@ use std::{path::Path, path::PathBuf};
|
||||
//use toml;
|
||||
|
||||
use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
|
||||
use drk::cli::{CashierdCli, CashierdConfig};
|
||||
use drk::cli::{CashierdCli, CashierdConfig, Config};
|
||||
use drk::service::CashierService;
|
||||
use drk::wallet::{WalletDb, WalletPtr};
|
||||
|
||||
@@ -35,41 +35,44 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&CashierdConfig>) -> Res
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use simplelog::*;
|
||||
|
||||
let ex = Arc::new(Executor::new());
|
||||
let (signal, shutdown) = async_channel::unbounded::<()>();
|
||||
|
||||
let path = join_config_path(&PathBuf::from("cashierd.toml")).unwrap();
|
||||
|
||||
let config: CashierdConfig = if Path::new(&path).exists() {
|
||||
CashierdConfig::load(path)?
|
||||
Config::<CashierdConfig>::load(path)?
|
||||
} else {
|
||||
CashierdConfig::load_default(path)?
|
||||
Config::<CashierdConfig>::load_default(path)?
|
||||
};
|
||||
|
||||
let config_ptr = Arc::new(&config);
|
||||
|
||||
let options = CashierdCli::load()?;
|
||||
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
{
|
||||
use simplelog::*;
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
|
||||
let ex2 = ex.clone();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
|
||||
use drk::cli::{DarkfidCli, DarkfidConfig};
|
||||
use drk::cli::{DarkfidCli, DarkfidConfig, Config};
|
||||
use drk::crypto::{
|
||||
load_params,
|
||||
merkle::{CommitmentTree, IncrementalWitness},
|
||||
@@ -220,16 +220,14 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&DarkfidConfig>) -> Resu
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use simplelog::*;
|
||||
|
||||
let options = Arc::new(DarkfidCli::load()?);
|
||||
|
||||
let path = join_config_path(&PathBuf::from("darkfid.toml")).unwrap();
|
||||
|
||||
let config: DarkfidConfig = if Path::new(&path).exists() {
|
||||
DarkfidConfig::load(path)?
|
||||
Config::<DarkfidConfig>::load(path)?
|
||||
} else {
|
||||
DarkfidConfig::load_default(path)?
|
||||
Config::<DarkfidConfig>::load_default(path)?
|
||||
};
|
||||
|
||||
let config_ptr = Arc::new(&config);
|
||||
@@ -237,24 +235,27 @@ fn main() -> Result<()> {
|
||||
let ex = Arc::new(Executor::new());
|
||||
let (signal, shutdown) = async_channel::unbounded::<()>();
|
||||
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
{
|
||||
use simplelog::*;
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let ex2 = ex.clone();
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use drk::cli::{DrkCli, DrkConfig};
|
||||
use drk::cli::{DrkCli, DrkConfig, Config};
|
||||
use drk::util::join_config_path;
|
||||
use drk::Result;
|
||||
use log::*;
|
||||
@@ -162,38 +162,39 @@ async fn start(config: &DrkConfig, options: DrkCli) -> Result<()> {
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use simplelog::*;
|
||||
|
||||
let options = DrkCli::load()?;
|
||||
|
||||
let path = join_config_path(&PathBuf::from("drk.toml")).unwrap();
|
||||
|
||||
let config: DrkConfig = if Path::new(&path).exists() {
|
||||
DrkConfig::load(path)?
|
||||
Config::<DrkConfig>::load(path)?
|
||||
} else {
|
||||
DrkConfig::load_default(path)?
|
||||
Config::<DrkConfig>::load_default(path)?
|
||||
};
|
||||
|
||||
let config_ptr = &config;
|
||||
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
{
|
||||
use simplelog::*;
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Info
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
futures::executor::block_on(start(config_ptr, options))?;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
|
||||
use drk::cli::{GatewaydCli, GatewaydConfig};
|
||||
use drk::cli::{Config, GatewaydCli, GatewaydConfig};
|
||||
use drk::service::GatewayService;
|
||||
use drk::util::join_config_path;
|
||||
use drk::Result;
|
||||
@@ -28,41 +28,42 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&GatewaydConfig>) -> Res
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use simplelog::*;
|
||||
|
||||
let ex = Arc::new(Executor::new());
|
||||
let (signal, shutdown) = async_channel::unbounded::<()>();
|
||||
|
||||
let path = join_config_path(&PathBuf::from("gatewayd.toml")).unwrap();
|
||||
|
||||
let config: GatewaydConfig = if Path::new(&path).exists() {
|
||||
GatewaydConfig::load(path)?
|
||||
Config::<GatewaydConfig>::load(path)?
|
||||
} else {
|
||||
GatewaydConfig::load_default(path)?
|
||||
Config::<GatewaydConfig>::load_default(path)?
|
||||
};
|
||||
|
||||
let config_ptr = Arc::new(&config);
|
||||
|
||||
let options = GatewaydCli::load()?;
|
||||
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
{
|
||||
use simplelog::*;
|
||||
let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
|
||||
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
let debug_level = if options.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
};
|
||||
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
let log_path = config.log_path.clone();
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
std::fs::File::create(log_path).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let ex2 = ex.clone();
|
||||
|
||||
|
||||
@@ -1,35 +1,31 @@
|
||||
use crate::util::join_config_path;
|
||||
use crate::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::str;
|
||||
use std::{
|
||||
fs,
|
||||
fs::{create_dir_all, File},
|
||||
io::Write,
|
||||
};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DrkConfig {
|
||||
#[serde(default)]
|
||||
pub rpc_url: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub log_path: String,
|
||||
pub struct Config<T> {
|
||||
config: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl DrkConfig {
|
||||
pub fn load(path: PathBuf) -> Result<Self> {
|
||||
impl<T: Default + Serialize + DeserializeOwned> Config<T> {
|
||||
pub fn load(path: PathBuf) -> Result<T> {
|
||||
let toml = fs::read(&path)?;
|
||||
let str_buff = str::from_utf8(&toml)?;
|
||||
let config: Self = toml::from_str(str_buff)?;
|
||||
let config: T = toml::from_str(str_buff.clone())?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn load_default(path: PathBuf) -> Result<Self> {
|
||||
let toml = Self::default();
|
||||
pub fn load_default(path: PathBuf) -> Result<T> {
|
||||
let toml = T::default();
|
||||
let config_file = toml::to_string(&toml)?;
|
||||
|
||||
if let Some(outdir) = path.parent() {
|
||||
@@ -44,6 +40,18 @@ impl DrkConfig {
|
||||
}
|
||||
}
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DrkConfig {
|
||||
#[serde(default)]
|
||||
pub rpc_url: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub log_path: String,
|
||||
}
|
||||
|
||||
|
||||
impl Default for DrkConfig {
|
||||
fn default() -> Self {
|
||||
let rpc_url = String::from("http://127.0.0.1:8000");
|
||||
@@ -79,29 +87,6 @@ pub struct DarkfidConfig {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl DarkfidConfig {
|
||||
pub fn load(path: PathBuf) -> Result<Self> {
|
||||
let toml = fs::read(&path)?;
|
||||
let str_buff = str::from_utf8(&toml)?;
|
||||
let config: Self = toml::from_str(str_buff)?;
|
||||
Ok(config)
|
||||
}
|
||||
pub fn load_default(path: PathBuf) -> Result<Self> {
|
||||
let toml = Self::default();
|
||||
let config_file = toml::to_string(&toml)?;
|
||||
|
||||
if let Some(outdir) = path.parent() {
|
||||
create_dir_all(outdir)?;
|
||||
}
|
||||
|
||||
let mut file = File::create(path.clone())?;
|
||||
file.write_all(&config_file.into_bytes())?;
|
||||
|
||||
let config = Self::load(path)?;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DarkfidConfig {
|
||||
fn default() -> Self {
|
||||
let connect_url = String::from("127.0.0.1:3333");
|
||||
@@ -113,8 +98,8 @@ impl Default for DarkfidConfig {
|
||||
.expect("error during join database_path to config path");
|
||||
let database_path = String::from(
|
||||
database_path
|
||||
.to_str()
|
||||
.expect("error convert Path to String"),
|
||||
.to_str()
|
||||
.expect("error convert Path to String"),
|
||||
);
|
||||
let log_path = String::from("/tmp/darkfid_service_daemon.log");
|
||||
|
||||
@@ -149,29 +134,6 @@ pub struct GatewaydConfig {
|
||||
pub log_path: String,
|
||||
}
|
||||
|
||||
impl GatewaydConfig {
|
||||
pub fn load(path: PathBuf) -> Result<Self> {
|
||||
let toml = fs::read(&path)?;
|
||||
let str_buff = str::from_utf8(&toml)?;
|
||||
let config: Self = toml::from_str(str_buff)?;
|
||||
Ok(config)
|
||||
}
|
||||
pub fn load_default(path: PathBuf) -> Result<Self> {
|
||||
let toml = Self::default();
|
||||
let config_file = toml::to_string(&toml)?;
|
||||
|
||||
if let Some(outdir) = path.parent() {
|
||||
create_dir_all(outdir)?;
|
||||
}
|
||||
|
||||
let mut file = File::create(path.clone())?;
|
||||
file.write_all(&config_file.into_bytes())?;
|
||||
|
||||
let config = Self::load(path)?;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GatewaydConfig {
|
||||
fn default() -> Self {
|
||||
let accept_url = String::from("127.0.0.1:3333");
|
||||
@@ -206,29 +168,6 @@ pub struct CashierdConfig {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl CashierdConfig {
|
||||
pub fn load(path: PathBuf) -> Result<Self> {
|
||||
let toml = fs::read(&path)?;
|
||||
let str_buff = str::from_utf8(&toml)?;
|
||||
let config: Self = toml::from_str(str_buff)?;
|
||||
Ok(config)
|
||||
}
|
||||
pub fn load_default(path: PathBuf) -> Result<Self> {
|
||||
let toml = Self::default();
|
||||
let config_file = toml::to_string(&toml)?;
|
||||
|
||||
if let Some(outdir) = path.parent() {
|
||||
create_dir_all(outdir)?;
|
||||
}
|
||||
|
||||
let mut file = File::create(path.clone())?;
|
||||
file.write_all(&config_file.into_bytes())?;
|
||||
|
||||
let config = Self::load(path)?;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CashierdConfig {
|
||||
fn default() -> Self {
|
||||
let accept_url = String::from("127.0.0.1:7777");
|
||||
|
||||
@@ -5,7 +5,7 @@ pub mod drk_cli;
|
||||
pub mod gatewayd_cli;
|
||||
|
||||
pub use cashierd_cli::CashierdCli;
|
||||
pub use cli_config::{CashierdConfig, DarkfidConfig, DrkConfig, GatewaydConfig};
|
||||
pub use cli_config::{CashierdConfig, DarkfidConfig, DrkConfig, GatewaydConfig, Config};
|
||||
pub use darkfid_cli::DarkfidCli;
|
||||
pub use drk_cli::DrkCli;
|
||||
pub use drk_cli::Transfer;
|
||||
|
||||
Reference in New Issue
Block a user