From 830a89cfd410147fdab888c09d97c72827b81ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 12 Nov 2020 16:47:00 +0000 Subject: [PATCH] Explorer public folder being relative to the binary (#447) * Explorer public folder being relative to the binary * Qualifying the public mounting path Co-authored-by: Dave --- explorer/src/jobs/mixmining.rs | 11 ++++++++++- explorer/src/jobs/topology.rs | 11 ++++++++++- explorer/src/main.rs | 8 +++++++- explorer/src/utils/file.rs | 6 +++--- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/explorer/src/jobs/mixmining.rs b/explorer/src/jobs/mixmining.rs index efc43ffa33..e55917c2c3 100644 --- a/explorer/src/jobs/mixmining.rs +++ b/explorer/src/jobs/mixmining.rs @@ -7,6 +7,15 @@ 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"); + + let save_path = std::env::current_exe() + .expect("Failed to evaluate current exe path") + .parent() + .expect("the binary itself has no parent path?!") + .join("public") + .join("downloads") + .join("mixmining.json"); + + file::save(topology_json, save_path); Ok(()) } diff --git a/explorer/src/jobs/topology.rs b/explorer/src/jobs/topology.rs index 680b8acafb..5d1f175a4e 100644 --- a/explorer/src/jobs/topology.rs +++ b/explorer/src/jobs/topology.rs @@ -7,6 +7,15 @@ 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"); + + let save_path = std::env::current_exe() + .expect("Failed to evaluate current exe path") + .parent() + .expect("the binary itself has no parent path?!") + .join("public") + .join("downloads") + .join("topology.json"); + + file::save(topology_json, save_path); Ok(()) } diff --git a/explorer/src/main.rs b/explorer/src/main.rs index 24df8945e0..475e84587b 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -37,9 +37,15 @@ async fn main() { let matches = parse_args(); let validator_base_url = matches.value_of(VALIDATOR_ARG).unwrap(); + let public_path = std::env::current_exe() + .expect("Failed to evaluate current exe path") + .parent() + .expect("the binary itself has no parent path?!") + .join("public"); + tokio::task::spawn_blocking(|| { rocket::ignite() - .mount("/", StaticFiles::from("public")) + .mount("/", StaticFiles::from(public_path)) .launch() }); diff --git a/explorer/src/utils/file.rs b/explorer/src/utils/file.rs index 33ac0dd7f0..929880b293 100644 --- a/explorer/src/utils/file.rs +++ b/explorer/src/utils/file.rs @@ -1,10 +1,10 @@ use std::{fs::File, io::Write, path::Path}; -pub fn save(text: String, path_str: &str) { - let path = Path::new(path_str); +pub fn save>(text: String, path: P) { + let path = path.as_ref(); let display = path.display(); - let mut file = match File::create(&path) { + let mut file = match File::create(path) { Err(why) => panic!("couldn't open {}: {}", display, why), Ok(file) => file, };