diff --git a/pkg/controller/chat/chat.go b/pkg/controller/chat/chat.go index c4ce8c3..bebcf65 100644 --- a/pkg/controller/chat/chat.go +++ b/pkg/controller/chat/chat.go @@ -98,7 +98,7 @@ func Landing() http.HandlerFunc { } // Country flag emoji. - emoji, err := geoip.GetRequestCountryFlag(r) + emoji, err := geoip.GetRequestCountryFlagWithCode(r) if err != nil { emoji, err = geoip.CountryFlagEmojiWithCode("US") if err != nil { diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index b562a70..eb2983c 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -36,6 +36,21 @@ func GetRequestCountryFlag(r *http.Request) (string, error) { return CountryFlagEmoji(city.Country.IsoCode) } +// GetRequestCountryFlagWithCode returns the flag joined with the country code by a space (like CountryFlagEmojiWithCode). +func GetRequestCountryFlagWithCode(r *http.Request) (string, error) { + city, err := GetRequestCity(r) + if err != nil { + // If the remote addr is localhost (local dev testing), default to US flag. + if addr := utility.IPAddress(r); addr == "127.0.0.1" || addr == "::1" { + return CountryFlagEmojiWithCode("US") + } + + return "", err + } + + return CountryFlagEmojiWithCode(city.Country.IsoCode) +} + // GetCity queries the GeoIP database for city information for an IP address. func GetCity(ip net.IP) (*geoip2.City, error) { db, err := geoip2.Open(config.GeoIPPath)