first commit
This commit is contained in:
37
internal/data/ipdb.go
Normal file
37
internal/data/ipdb.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"sync"
|
||||
"thuanle.me/ip-info/configs"
|
||||
)
|
||||
import reader "github.com/oschwald/maxminddb-golang"
|
||||
|
||||
type IpDb struct {
|
||||
r *reader.Reader
|
||||
}
|
||||
|
||||
var (
|
||||
ins *IpDb
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func Ins() *IpDb {
|
||||
once.Do(func() {
|
||||
ins = &IpDb{}
|
||||
_ = ins.Reload()
|
||||
})
|
||||
return ins
|
||||
}
|
||||
|
||||
func (d *IpDb) Reload() error {
|
||||
r, err := reader.Open(configs.MmdbDbFile)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to open mmdb")
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
d.r = r
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user