From 7e581183f465e1845bb802aba58542a28e703324 Mon Sep 17 00:00:00 2001 From: Mark Sinclair Date: Tue, 5 Oct 2021 17:41:38 +0100 Subject: [PATCH] Network Explorer API: add env var GEO_IP_SERVICE_API_KEY to provide an API key for https://freegeoip.app/ --- explorer-api/README.md | 6 +++++- explorer-api/src/country_statistics/mod.rs | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/explorer-api/README.md b/explorer-api/README.md index 49532965ca..d05c19064b 100644 --- a/explorer-api/README.md +++ b/explorer-api/README.md @@ -14,4 +14,8 @@ TODO: * grab mixnode description info via reqwest and serve it (avoid mixed-content errors) * serve it all over http * dependency injection -* tests \ No newline at end of file +* tests + +## Running + +- Supply the environment variable `GEO_IP_SERVICE_API_KEY` with a key from https://freegeoip.app/ \ No newline at end of file diff --git a/explorer-api/src/country_statistics/mod.rs b/explorer-api/src/country_statistics/mod.rs index 6f588e4eef..681cc7cc32 100644 --- a/explorer-api/src/country_statistics/mod.rs +++ b/explorer-api/src/country_statistics/mod.rs @@ -90,7 +90,15 @@ impl CountryStatistics { } async fn locate(ip: &str) -> Result { - let response = reqwest::get(format!("{}{}", crate::GEO_IP_SERVICE, ip)).await?; + let api_key = ::std::env::var("GEO_IP_SERVICE_API_KEY") + .expect("Env var GEO_IP_SERVICE_API_KEY is not set"); + let response = reqwest::get(format!( + "{}{}?apikey={}", + crate::GEO_IP_SERVICE, + ip, + api_key + )) + .await?; let location = response.json::().await?; Ok(location) }