@@ -9,10 +9,12 @@ import (
 // Database downloaded from http://dev.maxmind.com/geoip/geoip2/geolite2/
 const Database = "data/GeoLite2-Country.mmdb"
 
+// DetectCountry maps ip to country using default database
 func DetectCountry(ip string) (string, error) {
 	return DetectCountryWithDatabase(ip, Database)
 }
 
+// DetectCountryWithDatabase maps ip to country using custom database
 func DetectCountryWithDatabase(ip, database string) (string, error) {
 	db, err := geoip2.Open(database)
 	if err != nil {
package location

import (
	"github.com/oschwald/geoip2-golang"
	"github.com/pkg/errors"
	"net"
)

// Database downloaded from http://dev.maxmind.com/geoip/geoip2/geolite2/
const Database = "data/GeoLite2-Country.mmdb"

func DetectCountry(ip string) (string, error) {
	return DetectCountryWithDatabase(ip, Database)
}

func DetectCountryWithDatabase(ip, database string) (string, error) {
	db, err := geoip2.Open(database)
	if err != nil {
		return "", err
	}
	defer db.Close()

	ipObject := net.ParseIP(ip)
	countryRecord, err := db.Country(ipObject)
	if err != nil {
		return "", err
	}
	country := countryRecord.Country.IsoCode
	if country == "" {
		return "", errors.New("country was not found")
	}
	return country, nil
}

Code source has nothing common with environment there compiled application is executed - This config should be configured from CLI argument `--ip-database` OR - This config should be loader from `ConfigDirectory + "/GeoLite2-Country.mmdb"`