From 3ef40035ad9a680aebdea9e42fb96e248e43dcad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 12 Nov 2020 09:48:06 +0000 Subject: [PATCH] Passing validator base url as an argument (#439) --- explorer/Cargo.lock | 52 ++++++++++++++++++++++++++++++++++ explorer/Cargo.toml | 3 +- explorer/src/jobs/mixmining.rs | 13 ++++----- explorer/src/jobs/mod.rs | 6 ++-- explorer/src/jobs/topology.rs | 12 ++++---- explorer/src/main.rs | 19 ++++++++++++- 6 files changed, 87 insertions(+), 18 deletions(-) diff --git a/explorer/Cargo.lock b/explorer/Cargo.lock index ae158eee8c..c2adf4d422 100644 --- a/explorer/Cargo.lock +++ b/explorer/Cargo.lock @@ -55,6 +55,15 @@ dependencies = [ "opaque-debug 0.2.3", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "atty" version = "0.2.14" @@ -175,6 +184,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cookie" version = "0.11.3" @@ -937,6 +961,7 @@ dependencies = [ name = "nym-explorer" version = "0.1.0" dependencies = [ + "clap", "futures-util", "log 0.4.11", "reqwest", @@ -1458,6 +1483,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "subtle" version = "1.0.0" @@ -1506,6 +1537,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.44" @@ -1733,6 +1773,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -1790,6 +1836,12 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.1.5" diff --git a/explorer/Cargo.toml b/explorer/Cargo.toml index e318ed8de3..a3cc886e74 100644 --- a/explorer/Cargo.toml +++ b/explorer/Cargo.toml @@ -7,7 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# no point in importing entire futtures crate +clap = "2.33" +# no point in importing entire futures crate futures-util = "0.3" log = "0.4" reqwest = "0.10.8" diff --git a/explorer/src/jobs/mixmining.rs b/explorer/src/jobs/mixmining.rs index f4c33760fc..efc43ffa33 100644 --- a/explorer/src/jobs/mixmining.rs +++ b/explorer/src/jobs/mixmining.rs @@ -1,13 +1,12 @@ +use crate::utils::file; use reqwest::Error; -use crate::utils::file; +const RELATIVE_PATH: &str = "api/mixmining/fullreport"; -pub async fn renew_periodically() -> Result<(), Error> { - let topology_json = - reqwest::get("http://qa-validator.nymtech.net:8081/api/mixmining/fullreport") - .await? - .text() - .await?; +pub async fn renew_periodically(validator_base_url: &str) -> Result<(), Error> { + let url = format!("{}/{}", validator_base_url, RELATIVE_PATH); + + let topology_json = reqwest::get(&url).await?.text().await?; file::save(topology_json, "public/downloads/mixmining.json"); Ok(()) } diff --git a/explorer/src/jobs/mod.rs b/explorer/src/jobs/mod.rs index d22ff743e7..d0fbbd6e29 100644 --- a/explorer/src/jobs/mod.rs +++ b/explorer/src/jobs/mod.rs @@ -4,16 +4,16 @@ use tokio::time::{self, Duration}; mod mixmining; mod topology; -pub async fn start() { +pub async fn start(validator_base_url: &str) { let mut timer = time::interval(Duration::from_secs(10)); loop { timer.tick().await; - if let Err(err) = topology::renew_periodically().await { + if let Err(err) = topology::renew_periodically(validator_base_url).await { warn!("Error refreshing topology: {}", err) }; - if let Err(err) = mixmining::renew_periodically().await { + if let Err(err) = mixmining::renew_periodically(validator_base_url).await { warn!("Error refreshing mixmining report: {}", err) }; } diff --git a/explorer/src/jobs/topology.rs b/explorer/src/jobs/topology.rs index 4fab1084ea..680b8acafb 100644 --- a/explorer/src/jobs/topology.rs +++ b/explorer/src/jobs/topology.rs @@ -1,12 +1,12 @@ +use crate::utils::file; use reqwest::Error; -use crate::utils::file; +const RELATIVE_PATH: &str = "api/mixmining/topology"; -pub async fn renew_periodically() -> Result<(), Error> { - let topology_json = reqwest::get("http://qa-validator.nymtech.net:8081/api/mixmining/topology") - .await? - .text() - .await?; +pub async fn renew_periodically(validator_base_url: &str) -> Result<(), Error> { + let url = format!("{}/{}", validator_base_url, RELATIVE_PATH); + + let topology_json = reqwest::get(&url).await?.text().await?; file::save(topology_json, "public/downloads/topology.json"); Ok(()) } diff --git a/explorer/src/main.rs b/explorer/src/main.rs index a51ef25bbb..22ca829ed1 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -3,6 +3,7 @@ #[macro_use] extern crate rocket; +use clap::{App, Arg, ArgMatches}; use rocket_contrib::serve::StaticFiles; use tokio::sync::broadcast; @@ -12,6 +13,19 @@ mod websockets; // this specifies number of messages that can be held by the channel, not number of the clients. const BROADCAST_CAPACITY: usize = 10; +const VALIDATOR_ARG: &str = "validator"; + +fn parse_args<'a>() -> ArgMatches<'a> { + App::new("Nym Explorer") + .author("Nymtech") + .arg( + Arg::with_name(VALIDATOR_ARG) + .help("REST endpoint of the explorer will use to periodically grab topology and node status.") + .takes_value(true) + .required(true), + ) + .get_matches() +} #[get("/")] fn index() -> &'static str { @@ -20,6 +34,9 @@ fn index() -> &'static str { #[tokio::main] async fn main() { + let matches = parse_args(); + let validator_base_url = matches.value_of(VALIDATOR_ARG).unwrap(); + tokio::spawn(async move { rocket::ignite() .mount("/", StaticFiles::from("public")) @@ -39,5 +56,5 @@ async fn main() { websockets::listen(8080, sender_clone).await; }); - jobs::start().await; + jobs::start(validator_base_url).await; }