diff --git a/Cargo.lock b/Cargo.lock index 5ed1fdb..36452a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -769,7 +769,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "zlnk" -version = "0.4.1" +version = "0.4.2" dependencies = [ "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "maxminddb 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 780bbb8..84f9fa8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zlnk" -version = "0.4.1" +version = "0.4.2" authors = ["Jonas Kuche "] [dependencies] diff --git a/src/env_loader.rs b/src/env_loader.rs index f9f6d79..2124ab7 100644 --- a/src/env_loader.rs +++ b/src/env_loader.rs @@ -10,7 +10,8 @@ pub struct Env { pub short_alphabet: String, pub bad_request_message: String, pub mmdb_path: String, - pub trust_proxy: bool + pub trust_proxy: bool, + pub disable_stats: bool } const URL_REGEX: &'static str = r"^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/?$"; @@ -24,6 +25,7 @@ pub fn init() -> Env { short_alphabet: env::var("SHORT_ALPHABET").unwrap_or("hex".to_string()), bad_request_message: env::var("BAD_REQUEST_MESSAGE").unwrap_or("Ungültige URL".to_string()), mmdb_path: env::var("MMDB_PATH").unwrap_or("./GeoLite2-Country.mmdb".to_string()), - trust_proxy: env::var("TRUST_PROXY").is_ok() + trust_proxy: env::var("TRUST_PROXY").is_ok(), + disable_stats: env::var("DISABLE_STATS").is_ok() } } diff --git a/src/geo_locate_ip.rs b/src/geo_locate_ip.rs index 36c1342..4476849 100644 --- a/src/geo_locate_ip.rs +++ b/src/geo_locate_ip.rs @@ -3,17 +3,29 @@ use std::net::IpAddr; use std::str::FromStr; pub struct GeoLocateIP { - reader: Reader + reader: Option } impl GeoLocateIP { - pub fn new(path: String) -> Self { - let reader = Reader::open(&path).unwrap(); - GeoLocateIP {reader: reader} + pub fn new(path: String, enabled: bool) -> Self { + if enabled { + let reader = Reader::open(&path).unwrap(); + GeoLocateIP {reader: Some(reader)} + } else { + GeoLocateIP {reader: None} + } } pub fn locate(&self, ip_string: String) -> Option { let ip: IpAddr = FromStr::from_str(&ip_string).unwrap(); - let country = self.reader.lookup(ip); + let country; + match self.reader { + Some(ref reader) => { + country = reader.lookup(ip); + } + None => { + panic!("GEOReader not enabled!"); + } + } match country { Ok(result) => { let country: geoip2::Country = result; diff --git a/src/main.rs b/src/main.rs index 3090237..4dbc476 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,9 +63,9 @@ fn shorten(long_url: String, env: State, pool: State")] -fn longen(short: String, pool: State>, stats: Stats) -> Option { +fn longen(short: String, env: State, pool: State>, stats: Stats) -> Option { let connection = &pool.get().unwrap(); - let long = long(short, connection, stats); + let long = long(short, env.inner(), connection, stats); match long { Some(long) => { Some(Redirect::to(&long)) @@ -77,7 +77,10 @@ fn longen(short: String, pool: State>, stats: Stats } #[get("//stats")] -fn stats(short: String, pool: State>) -> Option> { +fn stats(short: String, env: State, pool: State>) -> Option> { + if env.disable_stats { + return None; + } let connection = &pool.get().unwrap(); let stats = Stats::stats_as_json(short, connection); match stats { @@ -103,7 +106,7 @@ fn bad_request(request: &Request) -> String { fn main() { let env = env_loader::init(); - let geo_locate_ip = GeoLocateIP::new(env.mmdb_path.clone()); + let geo_locate_ip = GeoLocateIP::new(env.mmdb_path.clone(), !env.disable_stats); let manager = RedisConnectionManager::new(env.redis_url.as_str()).unwrap(); let pool = r2d2::Pool::builder() .build(manager) diff --git a/src/shortener.rs b/src/shortener.rs index 94f8327..7791ca5 100644 --- a/src/shortener.rs +++ b/src/shortener.rs @@ -62,11 +62,13 @@ pub fn short(long_url: String, env: &Env, connection: &r2d2::PooledConnection, stats: Stats) -> Option { +pub fn long(short_url: String, env: &Env, connection: &r2d2::PooledConnection, stats: Stats) -> Option { let long = connection.get(format!("short_{}", &short_url)); match long { Ok(long) => { - stats.save(short_url, connection).unwrap(); + if !env.disable_stats { + stats.save(short_url, connection).unwrap(); + } Some(long) } Err(_err) => { diff --git a/src/stats.rs b/src/stats.rs index 5b19a52..9459192 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -43,6 +43,15 @@ impl<'a, 'r> FromRequest<'a, 'r> for Stats { type Error = (); fn from_request(request: &'a Request<'r>) -> request::Outcome { + let env = request.guard::>().unwrap(); + if env.disable_stats { + return Outcome::Success(Stats { + refer: "Unknown".to_string(), + browser: "Unknown".to_string(), + os: "Unknown".to_string(), + country: "Unknown".to_string() + }) + } let ip = request.guard::().unwrap(); let geo_locate_ip = request.guard::>().unwrap(); let refers: Vec<_> = request.headers().get("Referer").collect(); diff --git a/src/tests.rs b/src/tests.rs index fa8b2b7..44d840c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -28,7 +28,7 @@ fn shortener_test() { let _:() = redis::cmd("FLUSHDB").query(connection.deref()).unwrap(); let long_url = "https://zlnk.de".to_string(); let shorted = short(long_url.clone(), env, connection, None).unwrap(); - let longed = long(shorted, connection, stats).unwrap(); + let longed = long(shorted, env, connection, stats).unwrap(); assert_eq!(longed, long_url); }