geoip-api/src/main.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

2021-12-30 18:01:06 +00:00
use itertools::Itertools;
use std::{net::Ipv4Addr, sync::Arc};
2021-12-30 17:39:03 +00:00
use warp::Filter;
2021-12-30 18:01:06 +00:00
const IP2ASN_DATA: &str = include_str!("../data/ip2asn-v4.tsv");
#[derive(Debug)]
struct RangeRecord {
ip_start: Ipv4Addr,
ip_end: Ipv4Addr,
asn: u64,
country_code: &'static str,
asn_description: &'static str,
}
fn parse_ranges() -> Vec<RangeRecord> {
let mut ranges = Vec::with_capacity(IP2ASN_DATA.lines().count());
IP2ASN_DATA
.lines()
.filter(|s| !s.is_empty())
.map(|l| {
l.splitn(5, '\t')
.next_tuple::<(&str, &str, &str, &str, &str)>()
.unwrap()
})
.for_each(|(ip_start, ip_end, asn, country_code, asn_description)| {
ranges.push(RangeRecord {
ip_start: ip_start.parse().unwrap(),
ip_end: ip_end.parse().unwrap(),
asn: asn.parse().unwrap(),
country_code,
asn_description,
})
});
ranges
}
2021-12-30 17:39:03 +00:00
#[tokio::main]
async fn main() {
2021-12-30 18:01:06 +00:00
eprintln!("[i] Parsing IP ASN data...");
let _ranges = Arc::new(parse_ranges());
eprintln!("[i] Done!");
2021-12-30 17:39:03 +00:00
let hello = warp::any().map(|| "Hello!".to_string());
2021-12-30 18:01:06 +00:00
println!("\nListening on http://127.0.0.1:8000 ...");
2021-12-30 17:39:03 +00:00
warp::serve(hello).bind(([127, 0, 0, 1], 8000)).await;
2021-12-30 17:24:05 +00:00
}