mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58:00 +00:00
refactor(geoip): Open takes dir, derives canonical edition paths
Filenames are deterministic (<dir>/GeoLite2-City.mmdb, <dir>/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.
This commit is contained in:
parent
9b92136f91
commit
359b740cec
@ -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 {
|
||||
|
||||
@ -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 <dir>/GeoLite2-City.mmdb and <dir>/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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user