mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Create geoip plugin (#4688)
* Create geoip plugin Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Move DBFILE bullet below example Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove plugin name test case Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove languages option Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update free database link Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove last language bits Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Use 127.0.0.1 as probing IP Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/geoip.go Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/geoip.go Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Use relative path for fixtures dir Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Set names with default string zero value Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove unused db types Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove non city databases in testdata Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove create databases main Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Fix metadata label format test case Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Fix import path block Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * go fmt after changes Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Tidy up go.mod and go.sum Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Add plugin to CODEOWNERS Signed-off-by: Sven Nebel <nebel.sven@gmail.com> Co-authored-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
58
plugin/geoip/city.go
Normal file
58
plugin/geoip/city.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/coredns/coredns/plugin/metadata"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
const defaultLang = "en"
|
||||
|
||||
func (g GeoIP) setCityMetadata(ctx context.Context, data *geoip2.City) {
|
||||
// Set labels for city, country and continent names.
|
||||
cityName := data.City.Names[defaultLang]
|
||||
metadata.SetValueFunc(ctx, pluginName+"/city/name", func() string {
|
||||
return cityName
|
||||
})
|
||||
countryName := data.Country.Names[defaultLang]
|
||||
metadata.SetValueFunc(ctx, pluginName+"/country/name", func() string {
|
||||
return countryName
|
||||
})
|
||||
continentName := data.Continent.Names[defaultLang]
|
||||
metadata.SetValueFunc(ctx, pluginName+"/continent/name", func() string {
|
||||
return continentName
|
||||
})
|
||||
|
||||
countryCode := data.Country.IsoCode
|
||||
metadata.SetValueFunc(ctx, pluginName+"/country/code", func() string {
|
||||
return countryCode
|
||||
})
|
||||
isInEurope := strconv.FormatBool(data.Country.IsInEuropeanUnion)
|
||||
metadata.SetValueFunc(ctx, pluginName+"/country/is_in_european_union", func() string {
|
||||
return isInEurope
|
||||
})
|
||||
continentCode := data.Continent.Code
|
||||
metadata.SetValueFunc(ctx, pluginName+"/continent/code", func() string {
|
||||
return continentCode
|
||||
})
|
||||
|
||||
latitude := strconv.FormatFloat(float64(data.Location.Latitude), 'f', -1, 64)
|
||||
metadata.SetValueFunc(ctx, pluginName+"/latitude", func() string {
|
||||
return latitude
|
||||
})
|
||||
longitude := strconv.FormatFloat(float64(data.Location.Longitude), 'f', -1, 64)
|
||||
metadata.SetValueFunc(ctx, pluginName+"/longitude", func() string {
|
||||
return longitude
|
||||
})
|
||||
timeZone := data.Location.TimeZone
|
||||
metadata.SetValueFunc(ctx, pluginName+"/timezone", func() string {
|
||||
return timeZone
|
||||
})
|
||||
postalCode := data.Postal.Code
|
||||
metadata.SetValueFunc(ctx, pluginName+"/postalcode", func() string {
|
||||
return postalCode
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user