Browse Source

Merge pull request #6 from fspijkerman/extra-attributes

Extra placeholders
Andrey Blinov 6 years ago
parent
commit
41632e6012
3 changed files with 23 additions and 1 deletions
  1. 4 0
      README.md
  2. 7 1
      setup.go
  3. 12 0
      setup_test.go

+ 4 - 0
README.md

@@ -9,12 +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_city_geoname_id - GeoNameID of the city, example 146384
+  geoip_geohash - Geohash of latitude and longitude
 ```
 
 
@@ -40,6 +43,7 @@ proxy / localhost:3000 {
   header_upstream Latitude {geoip_latitude}
   header_upstream Longitude {geoip_longitude}
   header_upstream Time-Zone {geoip_time_zone}
+  header_upstream Geohash {geoip_geohash}
 }
 ```
 

+ 7 - 1
setup.go

@@ -11,6 +11,7 @@ import (
 	"github.com/mholt/caddy"
 	"github.com/mholt/caddy/caddyhttp/httpserver"
 	"github.com/oschwald/maxminddb-golang"
+	"github.com/mmcloughlin/geohash"
 	)
 
 // GeoIP represents a middleware instance
@@ -25,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 {
@@ -89,9 +92,12 @@ 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))
 	replacer.Set("geoip_time_zone", record.Location.TimeZone)
 
 	if rr, ok := w.(*httpserver.ResponseRecorder); ok {

+ 12 - 0
setup_test.go

@@ -69,4 +69,16 @@ func TestReplacers(t *testing.T) {
 	if got, want := rr.Replacer.Replace("{geoip_time_zone}"), "Asia/Nicosia"; got != want {
 		t.Errorf("Expected custom placeholder {geoip_time_zone} to be set (%s), but it wasn't; got: %s", want, got)
 	}
+
+	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)
+	}
 }