feat: support to get local ip with local-ip-address (#37)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2023-06-02 19:11:55 +08:00 committed by GitHub
parent 0ae97a36d4
commit 9aa02821db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -42,3 +42,4 @@ prometheus = "0.13.3"
warp = "0.3.5"
tokio = { version = "1.28.1", features = ["full"] }
hostname = "^0.3"
local-ip-address = "0.5.3"

View File

@ -67,7 +67,7 @@ async fn main() {
let _guards = init_tracing(NAME, &args.log_dir, args.log_level, None);
// Load config.
let config = Config::load(&args.config).unwrap();
let config = Config::load(&args.config).unwrap().convert().unwrap();
let metrics = Metrics::new(config.network.enable_ipv6);
metrics.serve().await;

View File

@ -18,9 +18,10 @@ use crate::config::{
default_cache_dir, default_config_dir, default_data_dir, default_lock_dir, default_log_dir,
default_plugin_dir, default_root_dir,
};
use local_ip_address::{local_ip, local_ipv6};
use serde::Deserialize;
use std::fs;
use std::net::{Ipv4Addr, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::time::Duration;
use tracing::info;
@ -120,9 +121,9 @@ pub struct Host {
// hostname is the hostname of the host.
pub hostname: String,
// TODO Implement default values of ipv4 and ipv6.
// ip is the advertise ip of the host.
// pub ip: IpAddr,
pub ip: IpAddr,
}
// Host implements default value for Host.
@ -132,6 +133,7 @@ impl Default for Host {
idc: None,
location: None,
hostname: hostname::get().unwrap().to_string_lossy().to_string(),
ip: local_ip().unwrap(),
}
}
}
@ -429,4 +431,13 @@ impl Config {
Ok(Self::default())
}
}
// convert converts the configuration.
pub fn convert(&mut self) -> Result<Config> {
if self.network.enable_ipv6 {
self.host.ip = local_ipv6().unwrap()
}
Ok(self.clone())
}
}