From fded8de2700302b754cd99d1780a04e9470480ad Mon Sep 17 00:00:00 2001 From: Janus Date: Fri, 16 Jul 2021 00:34:43 -0400 Subject: [PATCH] Update cashierd to reflect new toml config options --- src/bin/cashierd.rs | 63 +++++++++++-- src/cli/client_cli/cli_config.rs | 146 +++++++++++++++++++++++++++++++ src/cli/mod.rs | 2 +- 3 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 src/cli/client_cli/cli_config.rs diff --git a/src/bin/cashierd.rs b/src/bin/cashierd.rs index 06407dc15..ed8122435 100644 --- a/src/bin/cashierd.rs +++ b/src/bin/cashierd.rs @@ -1,10 +1,16 @@ use std::net::SocketAddr; +use std::str; use std::sync::Arc; +use std::fs::OpenOptions; +use std::io::Read; +use std::{fs, path::PathBuf}; +use toml; + use drk::blockchain::{rocks::columns, Rocks, RocksColumn}; -use drk::cli::ServiceCli; +use drk::cli::{ServiceCli, CashierdConfig}; use drk::service::CashierService; -// Testing only + use drk::util::join_config_path; use drk::Result; @@ -18,13 +24,15 @@ fn setup_addr(address: Option, default: SocketAddr) -> SocketAddr { } } -async fn start(executor: Arc>, options: ServiceCli) -> Result<()> { - let accept_addr: SocketAddr = setup_addr(options.accept_addr, "127.0.0.1:7777".parse()?); - //let pub_addr: SocketAddr = setup_addr(options.pub_addr, "127.0.0.1:8888".parse()?); - let database_path = options.database_path.clone(); +async fn start(executor: Arc>, config: Arc<&CashierdConfig>) -> Result<()> { + let accept_addr: SocketAddr = config.accept_url.parse()?; + + let database_path = config.database_path.clone(); + + let database_path = join_config_path(&PathBuf::from(database_path))?; - let database_path = join_config_path(&(*database_path))?; let rocks = Rocks::new(&database_path)?; + let rocks_cashierstore_column = RocksColumn::::new(rocks); // Use pw: PASSWORD for now //let cashier_wallet = Arc::new(WalletDB::new("cashier.db", "PASSWORD")?); @@ -35,6 +43,14 @@ async fn start(executor: Arc>, options: ServiceCli) -> Result<()> { Ok(()) } +fn set_default() -> Result { + let config_file = CashierdConfig { + accept_url: String::from("127.0.0.1:7777"), + database_path: String::from("cashierd.db"), + log_path: String::from("/tmp/cashierd.log"), + }; + Ok(config_file) +} fn main() -> Result<()> { use simplelog::*; @@ -42,6 +58,33 @@ fn main() -> Result<()> { let ex = Arc::new(Executor::new()); let (signal, shutdown) = async_channel::unbounded::<()>(); + let config_path = PathBuf::from("cashierd.toml"); + let path = join_config_path(&config_path).unwrap(); + + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(&path)?; + + let mut buffer: Vec = vec![]; + file.read_to_end(&mut buffer)?; + + if buffer.is_empty() { + // set the default setting + let config_file = set_default()?; + let config_file = toml::to_string(&config_file)?; + fs::write(&path, &config_file)?; + } + + // reload the config + let toml = fs::read(&path)?; + let str_buff = str::from_utf8(&toml)?; + + // read from config file + let config: CashierdConfig = toml::from_str(str_buff)?; + let config_pointer = Arc::new(&config); + let options = ServiceCli::load()?; let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build(); @@ -52,12 +95,14 @@ fn main() -> Result<()> { 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(options.log_path.as_path()).unwrap(), + std::fs::File::create(log_path).unwrap(), ), ]) .unwrap(); @@ -70,7 +115,7 @@ fn main() -> Result<()> { // Run the main future on the current thread. .finish(|| { smol::future::block_on(async move { - start(ex2, options).await?; + start(ex2, config_pointer).await?; drop(signal); Ok::<(), drk::Error>(()) }) diff --git a/src/cli/client_cli/cli_config.rs b/src/cli/client_cli/cli_config.rs new file mode 100644 index 000000000..86d55bf2c --- /dev/null +++ b/src/cli/client_cli/cli_config.rs @@ -0,0 +1,146 @@ +//use crate::serial::{deserialize, serialize, Decodable, Encodable}; +//use toml::{map::Map, Value}; +use crate::util::join_config_path; +//use crate::Result; + +use serde::{Deserialize, Serialize}; +//use log::*; + +use std::{fs::OpenOptions, io::prelude::*, path::PathBuf}; + +//pub trait ClientConfig: Default + Deserialize { +// fn load(path: PathBuf) -> Result { +// let path = join_config_path(&path)?; +// let mut file = OpenOptions::new() +// .read(true) +// .write(true) +// .create(true) +// .open(path)?; +// +// //let mut toml_map = Map::new(); +// let mut buffer = String::new(); +// file.read_to_string(&mut buffer)?; +// //let buffer: &'static str = buffer; +// +// //let tomlstring = toml::to_string(&file).expect("Could not encode TTOML value"); +// if !buffer.is_empty() { +// let config: Self = toml::from_str(&buffer)?; +// //let config = toml::to_string(&buffer).unwrap(); +// //let config: Self = deserialize(&buffer)?; +// //Ok(config) +// Ok(config) +// } else { +// Ok(Self::default()) +// } +// } +// fn save(&self, path: PathBuf) -> Result<()> { +// //let path = join_config_path(&path)?; +// //let mut file = OpenOptions::new().write(true).create(true).open(&path)?; +// //let serialized = serialize(self); +// //file.write_all(&serialized)?; +// Ok(()) +// } +//} +// +//impl ClientConfig for DrkConfig {} +//impl ClientConfig for DarkfidConfig {} + +#[derive(Serialize, Default, Deserialize, Debug)] +pub struct DrkConfig { + #[serde(rename = "rpc_url")] + pub rpc_url: String, + + #[serde(rename = "log_path")] + pub log_path: String, +} + +#[derive(Serialize, Default, Deserialize, Debug)] +pub struct DarkfidConfig { + #[serde(rename = "connect_url")] + pub connect_url: String, + + #[serde(rename = "subscriber_url")] + pub subscriber_url: String, + + #[serde(rename = "rpc_url")] + pub rpc_url: String, + + #[serde(rename = "database_path")] + pub database_path: String, + + #[serde(rename = "log_path")] + pub log_path: String, + + #[serde(rename = "password")] + pub password: String, +} + + +#[derive(Serialize, Default, Deserialize, Debug)] +pub struct GatewaydConfig { + #[serde(rename = "connect_url")] + pub accept_url: String, + + #[serde(rename = "subscriber_url")] + pub publisher_url: String, + + #[serde(rename = "database_path")] + pub database_path: String, + + #[serde(rename = "log_path")] + pub log_path: String, +} +#[derive(Serialize, Default, Deserialize, Debug)] +pub struct CashierdConfig { + #[serde(rename = "connect_url")] + pub accept_url: String, + + #[serde(rename = "database_path")] + pub database_path: String, + + #[serde(rename = "log_path")] + pub log_path: String, +} +//impl Default for DrkCliConfig { +// // default toml file +// fn default() -> Self { +// let rpc_url = String::from("http://127.0.0.1:8000"); +// let log_path = String::from("/tmp/drk_cli.log"); +// Self { +// rpc_url, +// log_path, +// } +// } +//} + +//impl Default for DarkfidCliConfig { +// // create default config file +// fn default() -> Self { +// //toml::toml! { +// // connect-url = "127.0.0.1:3333" +// //}; +// let connect_url = String::from("127.0.0.1:3333"); +// let subscriber_url = String::from("127.0.0.1:4444"); +// let rpc_url = String::from("127.0.0.1:8000"); +// +// let database_path = String::from("database_client.db"); +// let database_path = join_config_path(&PathBuf::from(database_path)) +// .expect("error during join database_path to config path"); +// let database_path = String::from( +// database_path +// .to_str() +// .expect("error convert Path to String"), +// ); +// let log_path = String::from("/tmp/darkfid_service_daemon.log"); +// +// let password = String::new(); +// Self { +// connect_url, +// subscriber_url, +// rpc_url, +// database_path, +// log_path, +// password, +// } +// } +//} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 68495ef84..7ef7be044 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -3,7 +3,7 @@ pub mod darkfid_cli; pub mod drk_cli; pub mod gatewayd_cli; -pub use cli_config::{DarkfidConfig, DrkConfig, GatewaydConfig}; +pub use cli_config::{DarkfidConfig, DrkConfig, CashierdConfig, GatewaydConfig}; pub use darkfid_cli::DarkfidCli; pub use drk_cli::DrkCli; pub use drk_cli::Transfer;