aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/config.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/util/config.rs b/src/util/config.rs
index cd65e009..9ff67711 100644
--- a/src/util/config.rs
+++ b/src/util/config.rs
@@ -2,7 +2,7 @@ use std::io::Read;
use std::net::SocketAddr;
use std::path::PathBuf;
-use serde::Deserialize;
+use serde::{de, Deserialize};
use crate::error::Error;
@@ -13,6 +13,7 @@ pub struct Config {
pub rpc_bind_addr: SocketAddr,
+ #[serde(deserialize_with = "deserialize_vec_addr")]
pub bootstrap_peers: Vec<SocketAddr>,
pub consul_host: Option<String>,
pub consul_service_name: Option<String>,
@@ -82,3 +83,28 @@ pub fn read_config(config_file: PathBuf) -> Result<Config, Error> {
Ok(toml::from_str(&config)?)
}
+
+fn deserialize_vec_addr<'de, D>(deserializer: D) -> Result<Vec<SocketAddr>, D::Error>
+where
+ D: de::Deserializer<'de>,
+{
+ use std::net::ToSocketAddrs;
+
+ Ok(<Vec<&str>>::deserialize(deserializer)?
+ .iter()
+ .filter_map(|&name| {
+ name.to_socket_addrs()
+ .map(|iter| (name, iter))
+ .map_err(|_| warn!("Error resolving \"{}\"", name))
+ .ok()
+ })
+ .map(|(name, iter)| {
+ let v = iter.collect::<Vec<_>>();
+ if v.is_empty() {
+ warn!("Error resolving \"{}\"", name)
+ }
+ v
+ })
+ .flatten()
+ .collect())
+}