From 9be3902087c16e64c5edaf9281849e4f6ff0b1e7 Mon Sep 17 00:00:00 2001 From: dynco-nym <173912580+dynco-nym@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:35:00 +0200 Subject: [PATCH] Remove config in favor of clap args --- nym-node-status-api/src/cli/mod.rs | 2 +- nym-node-status-api/src/config.rs | 73 -------------------------- nym-node-status-api/src/main.rs | 18 +++---- nym-node-status-api/src/monitor/mod.rs | 14 +++-- 4 files changed, 13 insertions(+), 94 deletions(-) delete mode 100644 nym-node-status-api/src/config.rs diff --git a/nym-node-status-api/src/cli/mod.rs b/nym-node-status-api/src/cli/mod.rs index 439f5362d8b..2d044846a6f 100644 --- a/nym-node-status-api/src/cli/mod.rs +++ b/nym-node-status-api/src/cli/mod.rs @@ -9,7 +9,7 @@ fn pretty_build_info_static() -> &'static str { PRETTY_BUILD_INFORMATION.get_or_init(|| bin_info!().pretty_print()) } -#[derive(Parser, Debug)] +#[derive(Clone, Debug, Parser)] #[clap(author = "Nymtech", version, long_version = pretty_build_info_static(), about)] pub(crate) struct Cli { /// Path pointing to an env file that configures the Nym API. diff --git a/nym-node-status-api/src/config.rs b/nym-node-status-api/src/config.rs deleted file mode 100644 index cc532f298a6..00000000000 --- a/nym-node-status-api/src/config.rs +++ /dev/null @@ -1,73 +0,0 @@ -use reqwest::Url; -use serde::Deserialize; -use std::time::Duration; - -use crate::cli::Cli; - -#[derive(Debug, Clone, Deserialize)] -pub(crate) struct Config { - #[serde(default = "Config::default_http_cache_seconds")] - nym_http_cache_ttl: u64, - #[serde(default = "Config::default_http_port")] - http_port: u16, - #[serde(rename = "nyxd")] - nyxd_addr: Url, - #[serde(default = "Config::default_client_timeout")] - #[serde(deserialize_with = "parse_duration")] - explorer_client_timeout: Duration, -} - -impl Config { - pub(crate) fn from_args(args: Cli) -> Self { - Config { - nym_http_cache_ttl: args.nym_http_cache_ttl, - http_port: args.http_port, - nyxd_addr: args.nyxd_addr, - explorer_client_timeout: args.explorer_client_timeout, - } - } - - pub(crate) fn from_env() -> anyhow::Result { - envy::from_env::().map_err(|e| { - tracing::error!("Failed to load config from env: {e}"); - anyhow::Error::from(e) - }) - } - - fn default_client_timeout() -> Duration { - Duration::from_secs(15) - } - - fn default_http_port() -> u16 { - 8000 - } - - fn default_http_cache_seconds() -> u64 { - 30 - } - - pub(crate) fn nym_http_cache_ttl(&self) -> u64 { - self.nym_http_cache_ttl - } - - pub(crate) fn http_port(&self) -> u16 { - self.http_port - } - - pub(crate) fn nyxd_addr(&self) -> &Url { - &self.nyxd_addr - } - - pub(crate) fn nym_explorer_client_timeout(&self) -> Duration { - self.explorer_client_timeout.to_owned() - } -} - -fn parse_duration<'de, D>(deserializer: D) -> Result -where - D: serde::Deserializer<'de>, -{ - let s: String = Deserialize::deserialize(deserializer)?; - let secs: u64 = s.parse().map_err(serde::de::Error::custom)?; - Ok(Duration::from_secs(secs)) -} diff --git a/nym-node-status-api/src/main.rs b/nym-node-status-api/src/main.rs index 9ae561e483b..a15cfe68a4c 100644 --- a/nym-node-status-api/src/main.rs +++ b/nym-node-status-api/src/main.rs @@ -1,9 +1,7 @@ use clap::Parser; -use nym_network_defaults::setup_env; use nym_task::signal::wait_for_signal; mod cli; -mod config; mod db; mod http; mod logging; @@ -14,31 +12,27 @@ async fn main() -> anyhow::Result<()> { logging::setup_tracing_logger(); let args = cli::Cli::parse(); - // if dotenv file is present, load its values - // otherwise, default to mainnet - setup_env(args.config_env_file.as_ref()); let connection_url = args.connection_url.clone(); - let conf = config::Config::from_args(args); - tracing::debug!("Using config:\n{:#?}", conf); + tracing::debug!("Using config:\n{:#?}", args); let storage = db::Storage::init(connection_url).await?; let db_pool = storage.pool_owned().await; - let conf_clone = conf.clone(); + let args_clone = args.clone(); tokio::spawn(async move { - monitor::spawn_in_background(db_pool, conf_clone).await; + monitor::spawn_in_background(db_pool, args_clone).await; }); tracing::info!("Started monitor task"); let shutdown_handles = http::server::start_http_api( storage.pool_owned().await, - conf.http_port(), - conf.nym_http_cache_ttl(), + args.http_port, + args.nym_http_cache_ttl, ) .await .expect("Failed to start server"); - tracing::info!("Started HTTP server on port {}", conf.http_port()); + tracing::info!("Started HTTP server on port {}", args.http_port); wait_for_signal().await; diff --git a/nym-node-status-api/src/monitor/mod.rs b/nym-node-status-api/src/monitor/mod.rs index 5079af3b5a5..3c5172c8a9f 100644 --- a/nym-node-status-api/src/monitor/mod.rs +++ b/nym-node-status-api/src/monitor/mod.rs @@ -1,4 +1,4 @@ -use crate::config::Config; +use crate::cli::Cli; use crate::db::models::{ gateway, mixnode, GatewayRecord, MixnodeRecord, NetworkSummary, GATEWAYS_BLACKLISTED_COUNT, GATEWAYS_BONDED_COUNT, GATEWAYS_EXPLORER_COUNT, GATEWAYS_HISTORICAL_COUNT, @@ -29,7 +29,7 @@ static DELEGATION_PROGRAM_WALLET: &str = "n1rnxpdpx3kldygsklfft0gech7fhfcux4zst5 // TODO dz: query many NYM APIs: // multiple instances running directory cache, ask sachin -pub(crate) async fn spawn_in_background(db_pool: DbPool, config: Config) -> JoinHandle<()> { +pub(crate) async fn spawn_in_background(db_pool: DbPool, config: Cli) -> JoinHandle<()> { let network_defaults = nym_network_defaults::NymNetworkDetails::new_from_env(); loop { @@ -54,7 +54,7 @@ pub(crate) async fn spawn_in_background(db_pool: DbPool, config: Config) -> Join async fn run( pool: &DbPool, network_details: &NymNetworkDetails, - config: &Config, + config: &Cli, ) -> anyhow::Result<()> { let default_api_url = network_details .endpoints @@ -70,10 +70,8 @@ async fn run( let default_explorer_url = default_explorer_url.expect("explorer url missing in network config"); - let explorer_client = ExplorerClient::new_with_timeout( - default_explorer_url, - config.nym_explorer_client_timeout(), - )?; + let explorer_client = + ExplorerClient::new_with_timeout(default_explorer_url, config.explorer_client_timeout)?; let explorer_gateways = explorer_client .get_gateways() .await @@ -126,7 +124,7 @@ async fn run( .await .log_error("get_active_mixnodes")?; let delegation_program_members = - get_delegation_program_details(network_details, config.nyxd_addr()).await?; + get_delegation_program_details(network_details, &config.nyxd_addr).await?; // keep stats for later let count_bonded_mixnodes = mixnodes.len();