diff --git a/src/config.rs b/src/config.rs index ba9f548..095fae6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +//! Application configuration loaded from environment variables. + use std::env; use std::net::SocketAddr; diff --git a/src/db.rs b/src/db.rs index d42cc38..e6526b3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,3 +1,5 @@ +//! MaxMind MMDB database loading, shared state, and lookup functions. + use std::net::IpAddr; use std::path::Path; use std::sync::Arc; @@ -7,7 +9,7 @@ use maxminddb::Reader; use serde::Deserialize; use crate::models::{ - IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo, from_mmdb_record, + from_mmdb_record, IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo, }; pub type DbReader = Reader>; @@ -30,11 +32,24 @@ pub fn is_ready(db: &SharedDb) -> bool { } pub fn lookup(db: &SharedDb, ip: IpAddr) -> Option { + with_reader(db, ip, |result| { + let record: MmdbRecord<'_> = result.decode().ok()??; + Some(from_mmdb_record(ip, &record)) + })? +} + +/// Loads the shared reader, performs a lookup, and passes the raw lookup result +/// to `f` for decoding. This eliminates the repeated load-guard-lookup boilerplate +/// while letting callers choose their own deserialization type and lifetime. +fn with_reader( + db: &SharedDb, + ip: IpAddr, + f: impl FnOnce(maxminddb::LookupResult<'_, Vec>) -> R, +) -> Option { let guard = db.load(); let reader = Option::as_ref(&guard)?; let result = reader.lookup(ip).ok()?; - let record: MmdbRecord<'_> = result.decode().ok()??; - Some(from_mmdb_record(ip, &record)) + Some(f(result)) } // Lightweight deserialization structs for single-field endpoints. @@ -65,51 +80,38 @@ struct CityOnly<'a> { } pub fn lookup_proxy(db: &SharedDb, ip: IpAddr) -> Option { - let guard = db.load(); - let reader = Option::as_ref(&guard)?; - let result = reader.lookup(ip).ok()?; - let record: ProxyOnly = result.decode().ok()??; - Some(ProxyInfo { - is_proxy: record.proxy.is_proxy, - is_vpn: record.proxy.is_vpn, - is_tor: record.proxy.is_tor, - is_hosting: record.proxy.is_hosting, - is_cdn: record.proxy.is_cdn, - is_school: record.proxy.is_school, - is_anonymous: record.proxy.is_anonymous, - }) + with_reader(db, ip, |result| { + let record: ProxyOnly = result.decode().ok()??; + Some(record.proxy.into()) + })? } pub fn lookup_asn_number(db: &SharedDb, ip: IpAddr) -> Option { - let guard = db.load(); - let reader = Option::as_ref(&guard)?; - let result = reader.lookup(ip).ok()?; - let record: AsnOnly<'_> = result.decode().ok()??; - record.asn.autonomous_system_number + with_reader(db, ip, |result| { + let record: AsnOnly<'_> = result.decode().ok()??; + record.asn.autonomous_system_number + })? } pub fn lookup_asn_org(db: &SharedDb, ip: IpAddr) -> Option { - let guard = db.load(); - let reader = Option::as_ref(&guard)?; - let result = reader.lookup(ip).ok()?; - let record: AsnOnly<'_> = result.decode().ok()??; - record.asn.autonomous_system_organization.map(str::to_owned) + with_reader(db, ip, |result| { + let record: AsnOnly<'_> = result.decode().ok()??; + record.asn.autonomous_system_organization.map(str::to_owned) + })? } pub fn lookup_country_name(db: &SharedDb, ip: IpAddr) -> Option { - let guard = db.load(); - let reader = Option::as_ref(&guard)?; - let result = reader.lookup(ip).ok()?; - let record: CountryOnly<'_> = result.decode().ok()??; - record.country.names.english.map(str::to_owned) + with_reader(db, ip, |result| { + let record: CountryOnly<'_> = result.decode().ok()??; + record.country.names.english.map(str::to_owned) + })? } pub fn lookup_city_name(db: &SharedDb, ip: IpAddr) -> Option { - let guard = db.load(); - let reader = Option::as_ref(&guard)?; - let result = reader.lookup(ip).ok()?; - let record: CityOnly<'_> = result.decode().ok()??; - record.city.names.english.map(str::to_owned) + with_reader(db, ip, |result| { + let record: CityOnly<'_> = result.decode().ok()??; + record.city.names.english.map(str::to_owned) + })? } #[cfg(test)] diff --git a/src/error.rs b/src/error.rs index 5fb6b1c..79cab6a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,9 @@ +//! Application error types with HTTP status code mapping. + use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; +#[derive(Debug)] pub enum AppError { IpNotFound, InvalidIp, @@ -36,6 +39,14 @@ impl AppError { } } +impl std::fmt::Display for AppError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.message()) + } +} + +impl std::error::Error for AppError {} + impl IntoResponse for AppError { fn into_response(self) -> Response { (self.status_code(), self.message()).into_response() diff --git a/src/i18n.rs b/src/i18n.rs index 52b6eaf..8a639eb 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -1,3 +1,5 @@ +//! Internationalization support with PO-file-based translations and locale negotiation. + use std::collections::HashMap; use std::sync::LazyLock; diff --git a/src/ip_validate.rs b/src/ip_validate.rs index c0b2051..40d6b33 100644 --- a/src/ip_validate.rs +++ b/src/ip_validate.rs @@ -1,3 +1,5 @@ +//! IP address validation: determines whether an address is publicly routable. + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// Returns `true` if the given IPv4 address is a globally routable public address. diff --git a/src/middleware.rs b/src/middleware.rs index 8f91dbe..46adb27 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -1,3 +1,5 @@ +//! Axum middleware for applying security headers to responses. + use axum::extract::Request; use axum::http::header::{self, HeaderValue}; use axum::middleware::Next; diff --git a/src/routes.rs b/src/routes.rs index bec1f2a..6775682 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,3 +1,5 @@ +//! Axum router construction with host-based routing for IPv4 and main domains. + use std::sync::Arc; use axum::Router; diff --git a/src/static_assets.rs b/src/static_assets.rs index 661fa31..f54a4ae 100644 --- a/src/static_assets.rs +++ b/src/static_assets.rs @@ -1,3 +1,5 @@ +//! Embedded static file serving with immutable cache headers. + use std::collections::HashMap; use std::sync::LazyLock; diff --git a/src/ua_detect.rs b/src/ua_detect.rs index a61446e..0da5ba3 100644 --- a/src/ua_detect.rs +++ b/src/ua_detect.rs @@ -1,3 +1,5 @@ +//! User-agent detection for distinguishing CLI/HTTP-library clients from browsers. + const CLI_PREFIXES: &[&str] = &[ "curl/", "Wget/", @@ -53,16 +55,16 @@ const HTTP_LIB_PREFIXES: &[&str] = &[ "mint/", ]; +fn matches_any_prefix(user_agent: &str, prefixes: &[&str]) -> bool { + prefixes.iter().any(|prefix| user_agent.starts_with(prefix)) +} + fn is_cli_user_agent(user_agent: &str) -> bool { - CLI_PREFIXES - .iter() - .any(|prefix| user_agent.starts_with(prefix)) + matches_any_prefix(user_agent, CLI_PREFIXES) } fn is_http_lib_user_agent(user_agent: &str) -> bool { - HTTP_LIB_PREFIXES - .iter() - .any(|prefix| user_agent.starts_with(prefix)) + matches_any_prefix(user_agent, HTTP_LIB_PREFIXES) } pub fn is_plain_text_agent(user_agent: &str) -> bool { diff --git a/src/updater.rs b/src/updater.rs index 87fe44c..76ce8f0 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,3 +1,5 @@ +//! Background database updater: downloads and hot-swaps the MMDB database on a daily schedule. + use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::{Duration, SystemTime, UNIX_EPOCH};