Browse Source

Stop crash with invalid/missing geoip data. Fixes #414

pull/431/head
Gabe Kangas 5 years ago
parent
commit
62d85a8b1d
  1. 17
      geoip/geoip.go

17
geoip/geoip.go

@ -29,6 +29,14 @@ func GetGeoFromIP(ip string) *GeoDetails { @@ -29,6 +29,14 @@ func GetGeoFromIP(ip string) *GeoDetails {
return &cachedGeoDetails
}
if ip == "::1" || ip == "127.0.0.1" {
return &GeoDetails{
CountryCode: "N/A",
RegionName: "Localhost",
TimeZone: "",
}
}
return nil
}
@ -72,9 +80,16 @@ func FetchGeoForIP(ip string) { @@ -72,9 +80,16 @@ func FetchGeoForIP(ip string) {
return
}
var regionName = "Unknown"
if len(record.Subdivisions) > 0 {
if region, ok := record.Subdivisions[0].Names["en"]; ok {
regionName = region
}
}
response := GeoDetails{
CountryCode: record.Country.IsoCode,
RegionName: record.Subdivisions[0].Names["en"],
RegionName: regionName,
TimeZone: record.Location.TimeZone,
}

Loading…
Cancel
Save