-
Notifications
You must be signed in to change notification settings - Fork 19
/
region.go
62 lines (49 loc) · 1.13 KB
/
region.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package region
import (
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"
)
var log = logging.Logger("region")
const (
unknown = "unknown"
separate = "-"
)
var defaultArea = ""
// Region represents a geo region interface
type Region interface {
GetGeoInfo(ip string) (*GeoInfo, error)
}
// GeoInfo represents the geo information
type GeoInfo struct {
Latitude float64
Longitude float64
IP string
Geo string
}
var region Region
// NewRegion initializes the geo region using the specified database path, geo type, and default area
func NewRegion(dbPath, geoType, area string) error {
var err error
defaultArea = area
switch geoType {
case TypeGeoLite():
region, err = InitGeoLite(dbPath)
default:
// panic("unknown Region type")
err = xerrors.New("unknown Region type")
}
return err
}
// GetRegion returns the current geo region
func GetRegion() Region {
return region
}
// DefaultGeoInfo creates a default GeoInfo object with the specified IP and default area
func DefaultGeoInfo(ip string) *GeoInfo {
return &GeoInfo{
Latitude: 0,
Longitude: 0,
IP: ip,
Geo: defaultArea,
}
}