first
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package background
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron"
|
||||
)
|
||||
|
||||
var c *cron.Cron
|
||||
|
||||
// RegisterCron - register all cron
|
||||
func RegisterCron() {
|
||||
c = cron.New()
|
||||
c.AddFunc("@daily", downloadGeoDB)
|
||||
c.Start()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user