Browse Source

Move to placeholders

Andrey Blinov 6 years ago
parent
commit
69dc5474c8
4 changed files with 26 additions and 113 deletions
  1. 7 75
      config.go
  2. 2 18
      config_test.go
  3. 17 10
      handler.go
  4. 0 10
      handler_test.go

+ 7 - 75
config.go

@@ -7,86 +7,18 @@ import (
 // Config specifies configuration parsed for Caddyfile
 type Config struct {
 	DatabasePath string
-
-	// Yout can set returned header names in config
-	// Country
-	HeaderNameCountryCode string
-	HeaderNameCountryIsEU string
-	HeaderNameCountryName string
-
-	// City
-	HeaderNameCityName string
-
-	// Location
-	HeaderNameLocationLat      string
-	HeaderNameLocationLon      string
-	HeaderNameLocationTimeZone string
-}
-
-// NewConfig initialize new Config with default values
-func NewConfig() Config {
-	c := Config{}
-
-	c.HeaderNameCountryCode = "X-Geoip-Country-Code"
-	c.HeaderNameCountryIsEU = "X-Geoip-Country-Eu"
-	c.HeaderNameCountryName = "X-Geoip-Country-Name"
-
-	c.HeaderNameCityName = "X-Geoip-City-Name"
-
-	c.HeaderNameLocationLat = "X-Geoip-Location-Lat"
-	c.HeaderNameLocationLon = "X-Geoip-Location-Lon"
-	c.HeaderNameLocationTimeZone = "X-Geoip-Location-Tz"
-	return c
 }
 
 func parseConfig(c *caddy.Controller) (Config, error) {
-	var config = NewConfig()
+	var config = Config{}
 	for c.Next() {
-		for c.NextBlock() {
-			value := c.Val()
-
-			switch value {
-			case "database":
-				if !c.NextArg() {
-					continue
-				}
-				config.DatabasePath = c.Val()
-			case "set_header_country_code":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameCountryCode = c.Val()
-			case "set_header_country_name":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameCountryName = c.Val()
-			case "set_header_country_eu":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameCountryIsEU = c.Val()
-			case "set_header_city_name":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameCityName = c.Val()
-			case "set_header_location_lat":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameLocationLat = c.Val()
-			case "set_header_location_lon":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameLocationLon = c.Val()
-			case "set_header_location_tz":
-				if !c.NextArg() {
-					continue
-				}
-				config.HeaderNameLocationTimeZone = c.Val()
+		value := c.Val()
+		switch value {
+		case "geoip":
+			if !c.NextArg() {
+				continue
 			}
+			config.DatabasePath = c.Val()
 		}
 	}
 	return config, nil

+ 2 - 18
config_test.go

@@ -10,30 +10,14 @@ import (
 func TestParseConfig(t *testing.T) {
 	controller := caddy.NewTestController("http", `
 		localhost:8080
-		geoip {
-			database path/to/maxmind/db
-			set_header_country_code Code
-			set_header_country_name CountryName
-			set_header_country_eu Eu
-			set_header_city_name CityName
-			set_header_location_lat Lat
-			set_header_location_lon Lon
-			set_header_location_tz TZ
-		}
+		geoip path/to/maxmind/db
 	`)
 	actual, err := parseConfig(controller)
 	if err != nil {
 		t.Errorf("parseConfig return err: %v", err)
 	}
 	expected := Config{
-		DatabasePath:               "path/to/maxmind/db",
-		HeaderNameCountryCode:      "Code",
-		HeaderNameCountryName:      "CountryName",
-		HeaderNameCountryIsEU:      "Eu",
-		HeaderNameCityName:         "CityName",
-		HeaderNameLocationLat:      "Lat",
-		HeaderNameLocationLon:      "Lon",
-		HeaderNameLocationTimeZone: "TZ",
+		DatabasePath: "path/to/maxmind/db",
 	}
 	if !reflect.DeepEqual(expected, actual) {
 		t.Errorf("Expected %v actual %v", expected, actual)

+ 17 - 10
handler.go

@@ -5,12 +5,13 @@ import (
 	"log"
 	"net"
 	"net/http"
-	"strconv"
 	"strings"
+
+	"github.com/kodnaplakal/caddy/caddyhttp/httpserver"
 )
 
 func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
-	gip.addGeoIPHeaders(w, r)
+	gip.addPlaceholders(w, r)
 	return gip.Next.ServeHTTP(w, r)
 }
 
@@ -32,7 +33,7 @@ var record struct {
 	} `maxminddb:"location"`
 }
 
-func (gip GeoIP) addGeoIPHeaders(w http.ResponseWriter, r *http.Request) {
+func (gip GeoIP) addPlaceholders(w http.ResponseWriter, r *http.Request) {
 	clientIP, _ := getClientIP(r, true)
 
 	err := gip.DBHandler.Lookup(clientIP, &record)
@@ -40,15 +41,21 @@ func (gip GeoIP) addGeoIPHeaders(w http.ResponseWriter, r *http.Request) {
 		log.Println(err)
 	}
 
-	r.Header.Set(gip.Config.HeaderNameCountryCode, record.Country.ISOCode)
-	r.Header.Set(gip.Config.HeaderNameCountryIsEU, strconv.FormatBool(record.Country.IsInEuropeanUnion))
-	r.Header.Set(gip.Config.HeaderNameCountryName, record.Country.Names["en"])
+	if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
+		rr.Replacer.Set("geoip_country_code", record.Country.ISOCode)
+	}
+
+	// repl.Set("sd", record.Country.ISOCode)
+
+	r.Header.Set("ssdsd", record.Country.ISOCode)
+	// r.Header.Set(gip.Config.HeaderNameCountryIsEU, strconv.FormatBool(record.Country.IsInEuropeanUnion))
+	// r.Header.Set(gip.Config.HeaderNameCountryName, record.Country.Names["en"])
 
-	r.Header.Set(gip.Config.HeaderNameCityName, record.City.Names["en"])
+	// r.Header.Set(gip.Config.HeaderNameCityName, record.City.Names["en"])
 
-	r.Header.Set(gip.Config.HeaderNameLocationLat, strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
-	r.Header.Set(gip.Config.HeaderNameLocationLon, strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
-	r.Header.Set(gip.Config.HeaderNameLocationTimeZone, record.Location.TimeZone)
+	// r.Header.Set(gip.Config.HeaderNameLocationLat, strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
+	// r.Header.Set(gip.Config.HeaderNameLocationLon, strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
+	// r.Header.Set(gip.Config.HeaderNameLocationTimeZone, record.Location.TimeZone)
 }
 
 func getClientIP(r *http.Request, strict bool) (net.IP, error) {

+ 0 - 10
handler_test.go

@@ -19,16 +19,6 @@ func TestToResolveGeoip(t *testing.T) {
 
 	config := Config{}
 
-	config.HeaderNameCountryCode = "X-Geoip-Country-Code"
-	config.HeaderNameCountryIsEU = "X-Geoip-Country-Eu"
-	config.HeaderNameCountryName = "X-Geoip-Country-Name"
-
-	config.HeaderNameCityName = "X-Geoip-City-Name"
-
-	config.HeaderNameLocationLat = "X-Geoip-Location-Lat"
-	config.HeaderNameLocationLon = "X-Geoip-Location-Lon"
-	config.HeaderNameLocationTimeZone = "X-Geoip-Location-Tz"
-
 	var (
 		gotHeaders      http.Header
 		expectedHeaders = http.Header{