MaxMind组件封装了github.com/oschwald/maxminddb-golang
的GEO查询功能,能通过IP来确定国家、省市等地理信息。geo数据库文件需要用户自行获取(获取免费或收费的geo数据文件)。
GEO查询结果定义:
// Geo lookup result
type Geo struct {
Code string `json:"code"` // country/area code
Continent string `json:"-"` // continent name (en)
Country string `json:"country,omitempty"` // country/area name (en)
Province string `json:"province,omitempty"` // province name (en)
City string `json:"city,omitempty"` // city name(en)
// i18n name, default is en
I18n struct {
Continent string
Country string
Province string
City string
} `json:"-"`
}
mmd.GeoByIp() // 根据IP查询GEO信息
# 组件ID,默认maxMind,国家、城市数据文件至少需要设置一个
maxMind:
# 组件类名称
class: "@pgo/Client/MaxMind/Client"
# 国家数据文件(只包含国家数据,文件5M左右)
countryFile: "@app/conf/GeoLite2-Country.mmdb"
# 城市数据文件(包含省市等数据,文件50M左右)
cityFile: "@app/conf/GeoLite2-City.mmdb"
// curl -v http://127.0.0.1:8000/max-mind/geo-by-ip
func (m *MaxMindController) ActionGeoByIp() {
// 获取要解析的IP地址
ip := m.GetContext().ValidateParam("ip", "182.150.28.13").Do()
// 获取MaxMind的上下文件适配对象
mmd := m.GetObject(MaxMind.AdapterClass).(*MaxMind.Adapter)
// 解析IP的geo信息
geo := mmd.GeoByIp(ip)
m.OutputJson(geo, http.StatusOK)
}