maxmind reader in rust
Cargo.toml
toml
[dependencies]
maxminddb = "0.17"
serde_json = "1.0"
main.rs
rust
use std::net::IpAddr;
use std::str::FromStr;
use maxminddb::geoip2;
use serde_json;
fn get_ip_location(ip_str: &str) -> Result<String, serde_json::Error> {
let location = "./GeoLite2-City.mmdb";
let reader = maxminddb::Reader::open_readfile(location).unwrap();
let ip = IpAddr::from_str(ip_str).unwrap();
let city: geoip2::City = reader.lookup(ip).unwrap();
serde_json::to_string(&city)
}
fn main() -> Result<(), String> {
println!("{:#?}", get_ip_location("8.8.8.8"));
Ok(())
}