|
@@ -1,9 +1,9 @@
|
|
|
-use crate::serial::{Encodable, Decodable, serialize, deserialize};
|
|
|
|
|
-use crate::Result;
|
|
|
|
|
|
|
+use crate::serial::{deserialize, serialize, Decodable, Encodable};
|
|
|
use crate::util::join_config_path;
|
|
use crate::util::join_config_path;
|
|
|
|
|
+use crate::Result;
|
|
|
|
|
|
|
|
use std::{
|
|
use std::{
|
|
|
- fs::{create_dir_all, File},
|
|
|
|
|
|
|
+ fs::OpenOptions,
|
|
|
io::prelude::*,
|
|
io::prelude::*,
|
|
|
path::PathBuf,
|
|
path::PathBuf,
|
|
|
};
|
|
};
|
|
@@ -18,61 +18,56 @@ pub struct Config {
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
impl Default for Config {
|
|
|
fn default() -> Self {
|
|
fn default() -> Self {
|
|
|
- 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 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 log_path = String::from("/tmp/darkfid_service_daemon.log");
|
|
let log_path = String::from("/tmp/darkfid_service_daemon.log");
|
|
|
Self {
|
|
Self {
|
|
|
connect_url,
|
|
connect_url,
|
|
|
subscriber_url,
|
|
subscriber_url,
|
|
|
rpc_url,
|
|
rpc_url,
|
|
|
database_path,
|
|
database_path,
|
|
|
- log_path
|
|
|
|
|
|
|
+ log_path,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
impl Config {
|
|
|
pub fn load(path: PathBuf) -> Result<Config> {
|
|
pub fn load(path: PathBuf) -> Result<Config> {
|
|
|
- let path = join_config_path(&path)?;
|
|
|
|
|
|
|
+ let path = join_config_path(&path)?;
|
|
|
load_config_file(path)
|
|
load_config_file(path)
|
|
|
}
|
|
}
|
|
|
- pub fn save(&self, path: PathBuf) -> Result <()> {
|
|
|
|
|
- let path = join_config_path(&path)?;
|
|
|
|
|
|
|
+ pub fn save(&self, path: PathBuf) -> Result<()> {
|
|
|
|
|
+ let path = join_config_path(&path)?;
|
|
|
save_config_file(self, path)
|
|
save_config_file(self, path)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+pub fn load_config_file(config_file: PathBuf) -> Result<Config> {
|
|
|
|
|
+ let mut file = OpenOptions::new()
|
|
|
|
|
+ .read(true)
|
|
|
|
|
+ .write(true)
|
|
|
|
|
+ .create(true)
|
|
|
|
|
+ .open(config_file)?;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-pub fn load_config_file(config_file: PathBuf) -> Result<Config>
|
|
|
|
|
-{
|
|
|
|
|
-
|
|
|
|
|
- let mut file = File::open(config_file)?;
|
|
|
|
|
let mut buffer: Vec<u8> = vec![];
|
|
let mut buffer: Vec<u8> = vec![];
|
|
|
file.read_to_end(&mut buffer)?;
|
|
file.read_to_end(&mut buffer)?;
|
|
|
- let config: Config = deserialize(&buffer)?;
|
|
|
|
|
-
|
|
|
|
|
- Ok(config)
|
|
|
|
|
|
|
+ if !buffer.is_empty() {
|
|
|
|
|
+ let config: Config = deserialize(&buffer)?;
|
|
|
|
|
+ Ok(config)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Ok(Config::default())
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-pub fn save_config_file(config: &Config, config_file: PathBuf) -> Result<()>
|
|
|
|
|
-{
|
|
|
|
|
|
|
+pub fn save_config_file(config: &Config, config_file: PathBuf) -> Result<()> {
|
|
|
|
|
+ let mut file = OpenOptions::new().write(true).create(true).open(&config_file)?;
|
|
|
let serialized = serialize(config);
|
|
let serialized = serialize(config);
|
|
|
-
|
|
|
|
|
- if let Some(outdir) = config_file.parent() {
|
|
|
|
|
- create_dir_all(outdir)?;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- let mut file = File::create(config_file)?;
|
|
|
|
|
file.write_all(&serialized)?;
|
|
file.write_all(&serialized)?;
|
|
|
-
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
impl Encodable for Config {
|
|
impl Encodable for Config {
|
|
|
fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
|
|
fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
|
|
|
let mut len = 0;
|
|
let mut len = 0;
|
|
@@ -92,7 +87,7 @@ impl Decodable for Config {
|
|
|
subscriber_url: Decodable::decode(&mut d)?,
|
|
subscriber_url: Decodable::decode(&mut d)?,
|
|
|
rpc_url: Decodable::decode(&mut d)?,
|
|
rpc_url: Decodable::decode(&mut d)?,
|
|
|
database_path: Decodable::decode(&mut d)?,
|
|
database_path: Decodable::decode(&mut d)?,
|
|
|
- log_path: Decodable::decode(&mut d)?
|
|
|
|
|
|
|
+ log_path: Decodable::decode(&mut d)?,
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|