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

+ 2 - 0
README.md

@@ -15,6 +15,7 @@ The following placeholders are available:
   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.
 ```
 
 
@@ -40,6 +41,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}
 }
 ```
 

+ 2 - 0
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
@@ -92,6 +93,7 @@ func (gip GeoIP) lookupLocation(w http.ResponseWriter, r *http.Request) {
 	replacer.Set("geoip_city_name", record.City.Names["en"])
 	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 {

+ 4 - 0
setup_test.go

@@ -69,4 +69,8 @@ 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)
+	}
 }