This commit is contained in:
Jay
2018-11-06 10:06:17 +08:00
commit 1e3ba45665
318 changed files with 197956 additions and 0 deletions
+14
View File
@@ -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()
}
+41
View File
@@ -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
}