42 lines
835 B
Go
42 lines
835 B
Go
package geoip
|
|
|
|
import (
|
|
"net"
|
|
|
|
geoip2 "github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
var db *geoip2.Reader
|
|
|
|
// NewReader -
|
|
func NewReader() (*geoip2.Reader, error) {
|
|
var err error
|
|
db, err = geoip2.Open("/data/GeoLite2-City.mmdb")
|
|
return db, err
|
|
}
|
|
|
|
// GeoStruct -
|
|
type GeoStruct struct {
|
|
City string `json:"city"`
|
|
Timezone string `json:"timezone"`
|
|
Location struct {
|
|
Latitude float64 `json:"latitude"`
|
|
Longitude float64 `json:"longtitude"`
|
|
} `json:"location"`
|
|
}
|
|
|
|
// QueryGEO -
|
|
func QueryGEO(input string) (geo *GeoStruct, err error) {
|
|
ip := net.ParseIP(input)
|
|
record, err := db.City(ip)
|
|
if err != nil {
|
|
return
|
|
}
|
|
geo = &GeoStruct{}
|
|
geo.City = record.City.Names["en"]
|
|
geo.Timezone = record.Location.TimeZone
|
|
geo.Location.Latitude = record.Location.Latitude
|
|
geo.Location.Longitude = record.Location.Longitude
|
|
return
|
|
}
|