removed default config and added ConfigNotFound error

This commit is contained in:
lunar-mining
2021-09-09 09:09:19 +02:00
parent 124ded7ebd
commit 399dffa7e5
9 changed files with 34 additions and 166 deletions

View File

@@ -1,7 +1,7 @@
use async_std::sync::Arc;
use std::net::SocketAddr;
use std::{path::Path, path::PathBuf};
use std::path::PathBuf;
use drk::cli::{CashierdCli, CashierdConfig, Config};
use drk::service::CashierService;
@@ -63,11 +63,7 @@ fn main() -> Result<()> {
let path = join_config_path(&PathBuf::from("cashierd.toml")).unwrap();
let config: CashierdConfig = if Path::new(&path).exists() {
Config::<CashierdConfig>::load(path)?
} else {
Config::<CashierdConfig>::load_default(path)?
};
let config: CashierdConfig = Config::<CashierdConfig>::load(path)?;
let config = Arc::new(config);

View File

@@ -11,7 +11,6 @@ use easy_parallel::Parallel;
use async_std::sync::Arc;
use std::net::SocketAddr;
use std::path::Path;
use std::path::PathBuf;
async fn start(executor: Arc<Executor<'_>>, config: Arc<DarkfidConfig>) -> Result<()> {
@@ -70,11 +69,7 @@ fn main() -> Result<()> {
}
}
let config: DarkfidConfig = if Path::new(&config_path).exists() {
Config::<DarkfidConfig>::load(config_path)?
} else {
Config::<DarkfidConfig>::load_default(config_path)?
};
let config: DarkfidConfig = Config::<DarkfidConfig>::load(config_path)?;
let config = Arc::new(config);

View File

@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use serde_json::json;
@@ -153,11 +153,17 @@ fn main() -> Result<()> {
}
}
let config: DrkConfig = if Path::new(&config_path).exists() {
Config::<DrkConfig>::load(config_path)?
} else {
Config::<DrkConfig>::load_default(config_path)?
};
let config: DrkConfig = Config::<DrkConfig>::load(config_path)?;
//let config: DrkConfig = if Path::new(&config_path).exists() {
// Config::<DrkConfig>::load(config_path)?
//};
//if Path::new(&config_path).exists() {
// let config: DrkConfig = Config::<DrkConfig>::load(config_path)?
//}
//else {
// Error::NoConfigError
//};
{
use simplelog::*;

View File

@@ -6,7 +6,7 @@ use drk::cli::{Config, GatewaydCli, GatewaydConfig};
use drk::service::GatewayService;
use drk::util::join_config_path;
use drk::Result;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
extern crate clap;
use async_executor::Executor;
@@ -33,11 +33,7 @@ fn main() -> Result<()> {
let path = join_config_path(&PathBuf::from("gatewayd.toml")).unwrap();
let config: GatewaydConfig = if Path::new(&path).exists() {
Config::<GatewaydConfig>::load(path)?
} else {
Config::<GatewaydConfig>::load_default(path)?
};
let config: GatewaydConfig = Config::<GatewaydConfig>::load(path)?;
let config_ptr = Arc::new(&config);

View File

@@ -1,14 +1,11 @@
use crate::util::join_config_path;
use crate::Result;
use crate::{Result, Error};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use std::{
env, fs,
fs::{create_dir_all, File},
io::Write,
path::PathBuf,
fs,
path::{Path, PathBuf},
str,
};
@@ -16,27 +13,17 @@ pub struct Config<T> {
config: PhantomData<T>,
}
impl<T: Default + Serialize + DeserializeOwned> Config<T> {
impl<T: 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: T = toml::from_str(str_buff.clone())?;
Ok(config)
}
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() {
create_dir_all(outdir)?;
if Path::new(&path).exists() {
let toml = fs::read(&path)?;
let str_buff = str::from_utf8(&toml)?;
let config: T = toml::from_str(str_buff.clone())?;
Ok(config)
}
else {
Err(Error::ConfigNotFound)
}
let mut file = File::create(path.clone())?;
file.write_all(&config_file.into_bytes())?;
let config = Self::load(path)?;
Ok(config)
}
}
@@ -49,19 +36,6 @@ pub struct DrkConfig {
pub log_path: String,
}
impl Default for DrkConfig {
fn default() -> Self {
let rpc_url = String::from("http://127.0.0.1:8000");
let mut lp = PathBuf::new();
lp.push(env::temp_dir());
lp.push("drk_cli.log");
let log_path = String::from(lp.to_str().unwrap());
Self { rpc_url, log_path }
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DarkfidConfig {
#[serde(default)]
@@ -97,43 +71,6 @@ pub struct DarkfidConfig {
pub password: String,
}
impl Default for DarkfidConfig {
fn default() -> Self {
let connect_url = String::from("127.0.0.1:3333");
let subscriber_url = String::from("127.0.0.1:4444");
let cashier_url = String::from("127.0.0.1:7777");
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("join database_path to config path");
let database_path = String::from(database_path.to_str().expect("convert Path to String"));
let walletdb_path = String::from("walletdb.db");
let walletdb_path = join_config_path(&PathBuf::from(walletdb_path))
.expect("join walletdb_path to config path");
let walletdb_path = String::from(walletdb_path.to_str().expect("convert Path to String"));
let mut lp = PathBuf::new();
lp.push(env::temp_dir());
lp.push("darkfid_service_daemon.log");
let log_path = String::from(lp.to_str().unwrap());
let password = String::new();
Self {
connect_url,
subscriber_url,
cashier_url,
rpc_url,
database_path,
walletdb_path,
log_path,
password,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GatewaydConfig {
#[serde(default)]
@@ -153,26 +90,6 @@ pub struct GatewaydConfig {
pub log_path: String,
}
impl Default for GatewaydConfig {
fn default() -> Self {
let accept_url = String::from("127.0.0.1:3333");
let publisher_url = String::from("127.0.0.1:4444");
let database_path = String::from("gatewayd.db");
let mut lp = PathBuf::new();
lp.push(env::temp_dir());
lp.push("gatewayd.log");
let log_path = String::from(lp.to_str().unwrap());
Self {
accept_url,
publisher_url,
database_path,
log_path,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CashierdConfig {
#[serde(default)]
@@ -215,47 +132,3 @@ pub struct CashierdConfig {
#[serde(rename = "client_password")]
pub client_password: String,
}
impl Default for CashierdConfig {
fn default() -> Self {
let accept_url = String::from("127.0.0.1:7777");
let rpc_url = String::from("http://127.0.0.1:8000");
let gateway_url = String::from("127.0.0.1:3333");
let client_database_path = String::from("cashier_client_database.db");
let btc_endpoint = String::from("tcp://electrum.blockstream.info:50001");
let mut lp = PathBuf::new();
lp.push(env::temp_dir());
lp.push("cashierd.log");
let log_path = String::from(lp.to_str().unwrap());
let cashierdb_path = String::from("cashier.db");
let cashierdb_path = join_config_path(&PathBuf::from(cashierdb_path))
.expect("join walletdb_path to config path");
let cashierdb_path = String::from(cashierdb_path.to_str().expect("convert Path to String"));
let client_walletdb_path = String::from("cashier_client_walletdb.db");
let client_walletdb_path = join_config_path(&PathBuf::from(client_walletdb_path))
.expect("join walletdb_path to config path");
let client_walletdb_path = String::from(
client_walletdb_path
.to_str()
.expect("convert Path to String"),
);
let password = String::new();
let client_password = String::new();
Self {
accept_url,
rpc_url,
client_database_path,
btc_endpoint,
gateway_url,
log_path,
cashierdb_path,
client_walletdb_path,
password,
client_password,
}
}
}

View File

@@ -21,13 +21,13 @@ use super::ClientFailed;
use async_executor::Executor;
use bellman::groth16;
use bls12_381::Bls12;
use log::*;
use jsonrpc_core::IoHandler;
use async_std::sync::{Arc, Mutex};
use std::net::SocketAddr;
use std::path::PathBuf;
use log::*;
pub struct Client {
pub state: State,

View File

@@ -118,7 +118,7 @@ impl Decodable for MintRevealedValues {
}
pub fn setup_mint_prover() -> groth16::Parameters<Bls12> {
println!("Making random params...");
println!("Mint: Making random params...");
let start = Instant::now();
let params = {
let c = MintContract {

View File

@@ -198,7 +198,7 @@ impl Decodable for SpendRevealedValues {
}
pub fn setup_spend_prover() -> groth16::Parameters<Bls12> {
println!("Making random params...");
println!("Spend: Making random params...");
let start = Instant::now();
let params = {
let c = SpendContract {

View File

@@ -56,6 +56,7 @@ pub enum Error {
Base58DecodeError(String),
BadBTCAddress(String),
BtcClientError,
ConfigNotFound,
}
impl std::error::Error for Error {}
@@ -108,6 +109,7 @@ impl fmt::Display for Error {
Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
Error::BadBTCAddress(ref err) => write!(f, "could not parse BTC address: {}", err),
Error::BtcClientError => f.write_str("Unable to create Electrum Client"),
Error::ConfigNotFound => f.write_str("No config file detected. Please create a config file"),
}
}
}