diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2022-01-20 23:47:36 +0100 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2022-01-20 23:47:36 +0100 |
commit | db98de94f47a831334a82d2044d08ebb2274e8d9 (patch) | |
tree | 6e56853801ade97925663555e60cae27acfa01ed | |
parent | 60df0aee2e9283ae29f60596244e53c9790a3677 (diff) |
fail gracefully if error getting the ip record
-rw-r--r-- | main.go | 19 |
1 files changed, 10 insertions, 9 deletions
@@ -138,13 +138,13 @@ func (g *geodb) geolocateGateways(b *bonafide) { g.GatewayTree = kdtree.NewKDTree(gatewayPoints) } -func (g *geodb) getRecordForIP(ipstr string) *geoip2.City { +func (g *geodb) getRecordForIP(ipstr string) (*geoip2.City, error) { ip := net.ParseIP(ipstr) record, err := g.db.City(ip) if err != nil { - log.Fatal(err) + return record, err } - return record + return record, nil } func geolocateCity(city string) coordinates { @@ -181,11 +181,12 @@ type GeolocationJSON struct { func (jh *jsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { ipstr := getRemoteIP(req) - record := jh.geoipdb.getRecordForIP(ipstr) - sortedGateways := jh.geoipdb.sortGateways(record.Location.Latitude, record.Location.Longitude) - - hitsPerCountry.With(prometheus.Labels{"country": record.Country.IsoCode}).Inc() - + record, err := jh.geoipdb.getRecordForIP(ipstr) + sortedGateways := []string{""} + if err != nil { + sortedGateways = jh.geoipdb.sortGateways(record.Location.Latitude, record.Location.Longitude) + hitsPerCountry.With(prometheus.Labels{"country": record.Country.IsoCode}).Inc() + } data := &GeolocationJSON{ ipstr, record.Country.IsoCode, @@ -205,7 +206,7 @@ type txtHandler struct { func (th *txtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { ipstr := getRemoteIP(req) - record := th.geoipdb.getRecordForIP(ipstr) + record, _ := th.geoipdb.getRecordForIP(ipstr) fmt.Fprintf(w, "Your IP: %s\n", ipstr) fmt.Fprintf(w, "Your Country: %s\n", record.Country.IsoCode) |