diff options
author | Mike Raunsbæk <mike+git@raunsbaek.dk> | 2018-06-15 10:29:13 +0300 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2018-08-27 22:30:57 +0300 |
commit | ac4a9de770f7fc4c4abf04a1a42facfb2b332f8b (patch) | |
tree | 886e039f3ae096f3bfc11b8a391e28f87073128d /iputil | |
parent | b821b1efcca5cfc67c130a69422f9cbc00340b5d (diff) |
Added coordinates from City database
Diffstat (limited to 'iputil')
-rw-r--r-- | iputil/geo/geo.go | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/iputil/geo/geo.go b/iputil/geo/geo.go index 4f1705f..2b918e1 100644 --- a/iputil/geo/geo.go +++ b/iputil/geo/geo.go @@ -2,19 +2,27 @@ package geo import ( "net" + "math" geoip2 "github.com/oschwald/geoip2-golang" ) type Reader interface { Country(net.IP) (Country, error) - City(net.IP) (string, error) + City(net.IP) (City, error) IsEmpty() bool } type Country struct { - Name string - ISO string + Name string + ISO string + IsInEuropeanUnion bool +} + +type City struct { + Name string + Latitude float64 + Longitude float64 } type geoip struct { @@ -62,21 +70,32 @@ func (g *geoip) Country(ip net.IP) (Country, error) { if record.RegisteredCountry.IsoCode != "" && country.ISO == "" { country.ISO = record.RegisteredCountry.IsoCode } + country.IsInEuropeanUnion = record.Country.IsInEuropeanUnion + if record.RegisteredCountry.IsoCode != "" && country.ISO == "" { + country.IsInEuropeanUnion = record.RegisteredCountry.IsInEuropeanUnion + } return country, nil } -func (g *geoip) City(ip net.IP) (string, error) { +func (g *geoip) City(ip net.IP) (City, error) { + city := City{} if g.city == nil { - return "", nil + return city, nil } record, err := g.city.City(ip) if err != nil { - return "", err + return city, err } - if city, exists := record.City.Names["en"]; exists { - return city, nil + if c, exists := record.City.Names["en"]; exists { + city.Name = c + } + if !math.IsNaN(record.Location.Latitude) { + city.Latitude = record.Location.Latitude + } + if !math.IsNaN(record.Location.Longitude) { + city.Longitude = record.Location.Longitude } - return "", nil + return city, nil } func (g *geoip) IsEmpty() bool { |