-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommon_geobase.go
50 lines (47 loc) · 1.26 KB
/
common_geobase.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
package main
type geoItem struct {
Name string
RegID int
ID string
City string
Network string
TZ string
Country string
CountryCode string
}
// GeoBase interface for downloadable and convertible geo database
type GeoBase interface {
download() ([]byte, error)
unpack([]byte) error
citiesDB() (map[string]geoItem, error)
writeMap(map[string]geoItem) error
name() string
addError(Error)
}
// Generate GeoBase (download, unpack, parse and write in nginx map format)
func Generate(geobase GeoBase) {
answer, err := geobase.download()
if err != nil {
geobase.addError(Error{err, geobase.name(), "Download"})
return
}
printMessage(geobase.name(), "Download", "OK")
err = geobase.unpack(answer)
if err != nil {
geobase.addError(Error{err, geobase.name(), "Unpack"})
return
}
printMessage(geobase.name(), "Unpack", "OK")
cities, err := geobase.citiesDB()
if err != nil {
geobase.addError(Error{err, geobase.name(), "Generate Cities"})
return
}
printMessage(geobase.name(), "Generate cities", "OK")
if err := geobase.writeMap(cities); err != nil {
geobase.addError(Error{err, geobase.name(), "Write map"})
return
}
printMessage(geobase.name(), "Write nginx maps", "OK")
geobase.addError(Error{err: nil})
}