github.com/SupenBysz/gf-admin-community@v0.7.4/internal/boot/ip2region.go (about) 1 package boot 2 3 import ( 4 "context" 5 "github.com/SupenBysz/gf-admin-community/sys_consts" 6 "github.com/gogf/gf/v2/frame/g" 7 "github.com/gogf/gf/v2/os/gfile" 8 "github.com/kysion/base-library/utility/downloader" 9 "github.com/lionsoul2014/ip2region/binding/golang/xdb" 10 "log" 11 "os" 12 ) 13 14 func InitIp2region() { 15 workFolder, _ := os.Getwd() 16 folderPath := workFolder + "/resource/assets/static" 17 18 if !gfile.Exists(folderPath) { 19 _ = gfile.Mkdir(folderPath) 20 } 21 22 ip2regionPath := g.Cfg().MustGet( 23 context.Background(), 24 "service.ip2region.xdbPath", 25 folderPath+"/ip2region.xdb", 26 ).String() 27 28 if ip2regionPath == "" || gfile.Size(ip2regionPath) <= 0 { 29 log.Println("开始下载IP信息库资源") 30 d := downloader.NewDownloader( 31 "https://mirror.ghproxy.com/https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region.xdb", 32 gfile.Basename(ip2regionPath), 33 gfile.Abs(gfile.Dir(ip2regionPath)), 34 10, 35 ) 36 if err := d.Download(); err != nil { 37 panic("ip2region 获取失败") 38 } 39 } 40 if gfile.Size(ip2regionPath) <= 0 { 41 panic("ip2region 校验失败") 42 } 43 44 cBuff, err := xdb.LoadContentFromFile(ip2regionPath) 45 if err != nil { 46 panic("ip2region 初始化失败") 47 } 48 sys_consts.Global.Searcher, _ = xdb.NewWithBuffer(cBuff) 49 }