Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Include EU flag in response #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions apiserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,16 @@ func (q *geoipQuery) Record(ip net.IP, lang string) *responseRecord {
lang = parseAcceptLanguage(lang, q.Country.Names)

r := &responseRecord{
IP: ip.String(),
CountryCode: q.Country.ISOCode,
CountryName: q.Country.Names[lang],
City: q.City.Names[lang],
ZipCode: q.Postal.Code,
TimeZone: q.Location.TimeZone,
Latitude: roundFloat(q.Location.Latitude, .5, 4),
Longitude: roundFloat(q.Location.Longitude, .5, 4),
MetroCode: q.Location.MetroCode,
IP: ip.String(),
IsInEuropeanUnion: q.Country.IsInEuropeanUnion,
CountryCode: q.Country.ISOCode,
CountryName: q.Country.Names[lang],
City: q.City.Names[lang],
ZipCode: q.Postal.Code,
TimeZone: q.Location.TimeZone,
Latitude: roundFloat(q.Location.Latitude, .5, 4),
Longitude: roundFloat(q.Location.Longitude, .5, 4),
MetroCode: q.Location.MetroCode,
}
if len(q.Region) > 0 {
r.RegionCode = q.Region[0].ISOCode
Expand Down Expand Up @@ -285,24 +286,29 @@ func roundFloat(val float64, roundOn float64, places int) (newVal float64) {
}

type responseRecord struct {
XMLName xml.Name `xml:"Response" json:"-"`
IP string `json:"ip"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
RegionCode string `json:"region_code"`
RegionName string `json:"region_name"`
City string `json:"city"`
ZipCode string `json:"zip_code"`
TimeZone string `json:"time_zone"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
MetroCode uint `json:"metro_code"`
XMLName xml.Name `xml:"Response" json:"-"`
IP string `json:"ip"`
IsInEuropeanUnion bool `json:"is_in_european_union"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
RegionCode string `json:"region_code"`
RegionName string `json:"region_name"`
City string `json:"city"`
ZipCode string `json:"zip_code"`
TimeZone string `json:"time_zone"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
MetroCode uint `json:"metro_code"`
}

func (rr *responseRecord) String() string {
b := &bytes.Buffer{}
w := csv.NewWriter(b)
w.UseCRLF = true
var inEU int
if rr.IsInEuropeanUnion {
inEU = 1
}
w.Write([]string{
rr.IP,
rr.CountryCode,
Expand All @@ -315,6 +321,7 @@ func (rr *responseRecord) String() string {
strconv.FormatFloat(rr.Latitude, 'f', 4, 64),
strconv.FormatFloat(rr.Longitude, 'f', 4, 64),
strconv.Itoa(int(rr.MetroCode)),
strconv.Itoa(inEU),
})
w.Flush()
return b.String()
Expand Down
5 changes: 3 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ type DefaultQuery struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
Country struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
ISOCode string `maxminddb:"iso_code"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Region []struct {
ISOCode string `maxminddb:"iso_code"`
Expand Down