Browse Source

geoip_city_geoname_id and geoip_country_geoname_id

Frank Spijkerman 6 years ago
parent
commit
a08e454658
3 changed files with 16 additions and 2 deletions
  1. 3 1
      README.md
  2. 5 1
      setup.go
  3. 8 0
      setup_test.go

+ 3 - 1
README.md

@@ -9,13 +9,15 @@ The following placeholders are available:
 
 ```
   geoip_country_code - Country ISO code, example CY for Cyprus
+  geoip_country_geoname_id - GeoNameID of the city, example 146669
   geoip_latitude - Latitude, example 34.684100
   geoip_longitude - Longitude, example 33.037900
   geoip_time_zone - Time zone, example Asia/Nicosia
   geoip_country_eu - Return 'true' if country in Europen Union
   geoip_country_name - Full country name
   geoip_city_name - City name
-  geoip_geohash - Geohash of latitude and longitude.
+  geoip_city_geoname_id - GeoNameID of the city, example 146384
+  geoip_geohash - Geohash of latitude and longitude
 ```
 
 

+ 5 - 1
setup.go

@@ -26,10 +26,12 @@ type GeoIPRecord struct {
 		ISOCode           string            `maxminddb:"iso_code"`
 		IsInEuropeanUnion bool              `maxminddb:"is_in_european_union"`
 		Names             map[string]string `maxminddb:"names"`
+		GeoNameID		  uint64 			`maxminddb:"geoname_id"`
 	} `maxminddb:"country"`
 
 	City struct {
-		Names map[string]string `maxminddb:"names"`
+		Names     map[string]string `maxminddb:"names"`
+		GeoNameID uint64 			`maxminddb:"geoname_id"`
 	} `maxminddb:"city"`
 
 	Location struct {
@@ -90,7 +92,9 @@ func (gip GeoIP) lookupLocation(w http.ResponseWriter, r *http.Request) {
 	replacer.Set("geoip_country_code", record.Country.ISOCode)
 	replacer.Set("geoip_country_name", record.Country.Names["en"])
 	replacer.Set("geoip_country_eu", strconv.FormatBool(record.Country.IsInEuropeanUnion))
+	replacer.Set("geoip_country_geoname_id", strconv.FormatUint(record.Country.GeoNameID, 10))
 	replacer.Set("geoip_city_name", record.City.Names["en"])
+	replacer.Set("geoip_city_geoname_id", strconv.FormatUint(record.City.GeoNameID, 10))
 	replacer.Set("geoip_latitude", strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
 	replacer.Set("geoip_longitude", strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
 	replacer.Set("geoip_geohash", geohash.Encode(record.Location.Latitude, record.Location.Longitude))

+ 8 - 0
setup_test.go

@@ -73,4 +73,12 @@ func TestReplacers(t *testing.T) {
 	if got, want := rr.Replacer.Replace("{geoip_geohash}"), "swpmrf13wbgg"; got != want {
 		t.Errorf("Expected custom placeholder {geoip_geohash} to be set (%s), but it wasn't; got: %s", want, got)
 	}
+
+	if got, want := rr.Replacer.Replace("{geoip_city_geoname_id}"), "146384"; got != want {
+		t.Errorf("Expected custom placeholder {geoip_city_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
+	}
+
+	if got, want := rr.Replacer.Replace("{geoip_country_geoname_id}"), "146669"; got != want {
+		t.Errorf("Expected custom placeholder {geoip_country_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
+	}
 }