From 359b740cecb29ca7acc5f446ac78ea2927eee06d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 20 Apr 2026 16:12:46 -0600 Subject: [PATCH] refactor(geoip): Open takes dir, derives canonical edition paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filenames are deterministic (/GeoLite2-City.mmdb, /GeoLite2-ASN.mmdb) — callers no longer pass both paths. cmd/check-ip drops its cityPath/asnPath locals and just hands the maxmind dir to geoip.Open and the fetcher builder. --- cmd/check-ip/main.go | 10 +++++----- net/geoip/databases.go | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/check-ip/main.go b/cmd/check-ip/main.go index 0f6093d..9165b8a 100644 --- a/cmd/check-ip/main.go +++ b/cmd/check-ip/main.go @@ -103,11 +103,9 @@ func main() { } maxmind := filepath.Join(cfg.CacheDir, "maxmind") - cityPath := filepath.Join(maxmind, geoip.CityEdition+".mmdb") - asnPath := filepath.Join(maxmind, geoip.ASNEdition+".mmdb") - geoGroup := dataset.NewGroup(geoFetcher(cfg.ConfPath, cityPath, asnPath)) + geoGroup := dataset.NewGroup(geoFetcher(cfg.ConfPath, maxmind)) cfg.geo = dataset.Add(geoGroup, func() (*geoip.Databases, error) { - return geoip.Open(cityPath, asnPath) + return geoip.Open(maxmind) }) if err := geoGroup.Load(context.Background()); err != nil { log.Fatalf("geoip: %v", err) @@ -135,7 +133,9 @@ func main() { // With a GeoIP.conf (explicit path or auto-discovered) both files are // downloaded via httpcache conditional GETs; otherwise the files are // expected to exist on disk and are polled for out-of-band changes. -func geoFetcher(confPath, cityPath, asnPath string) dataset.Fetcher { +func geoFetcher(confPath, dir string) dataset.Fetcher { + cityPath := filepath.Join(dir, geoip.CityEdition+".mmdb") + asnPath := filepath.Join(dir, geoip.ASNEdition+".mmdb") if confPath == "" { for _, p := range geoip.DefaultConfPaths() { if _, err := os.Stat(p); err == nil { diff --git a/net/geoip/databases.go b/net/geoip/databases.go index e3c5274..4cbf806 100644 --- a/net/geoip/databases.go +++ b/net/geoip/databases.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/netip" + "path/filepath" "github.com/oschwald/geoip2-golang" ) @@ -14,8 +15,10 @@ type Databases struct { ASN *geoip2.Reader } -// Open opens city and ASN .mmdb files from the given paths. -func Open(cityPath, asnPath string) (*Databases, error) { +// Open opens /GeoLite2-City.mmdb and /GeoLite2-ASN.mmdb. +func Open(dir string) (*Databases, error) { + cityPath := filepath.Join(dir, CityEdition+".mmdb") + asnPath := filepath.Join(dir, ASNEdition+".mmdb") city, err := geoip2.Open(cityPath) if err != nil { return nil, fmt.Errorf("open %s: %w", cityPath, err)