first commit
This commit is contained in:
37
utils/geoip_util.go
Normal file
37
utils/geoip_util.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Package utils/geoip_util.go
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
var (
|
||||
geoDB *geoip2.Reader
|
||||
geoOnce sync.Once
|
||||
)
|
||||
|
||||
// InitGeoIP 初始化 GeoIP 数据库
|
||||
func InitGeoIP(geoDBPath string) error {
|
||||
var err error
|
||||
geoOnce.Do(func() {
|
||||
if geoDBPath != "" {
|
||||
geoDB, err = geoip2.Open(geoDBPath)
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// GetGeoIP 获取 GeoIP 实例
|
||||
func GetGeoIP() *geoip2.Reader {
|
||||
return geoDB
|
||||
}
|
||||
|
||||
// CloseGeoIP 关闭 GeoIP 数据库
|
||||
func CloseGeoIP() error {
|
||||
if geoDB != nil {
|
||||
return geoDB.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user