123456789101112131415161718192021222324252627282930313233343536373839 |
- package main
- import (
- "net"
- "net/http"
- "strings"
- "github.com/oschwald/geoip2-golang"
- )
- func getCountryCodeFromRequest(r *http.Request) string {
- countryCode := ""
-
- ipAddress := r.Header.Get("X-Forwarded-For")
- if ipAddress == "" {
- ipAddress = strings.Split(r.RemoteAddr, ":")[0]
- }
-
- db, err := geoip2.Open("./tmp/GeoIP2-Country.mmdb")
- if err != nil {
-
- return countryCode
- }
- defer db.Close()
-
- record, err := db.Country(net.ParseIP(ipAddress))
- if err != nil {
-
- return countryCode
- }
-
- countryCode = record.Country.IsoCode
- return countryCode
- }
|