-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
54 lines (43 loc) · 1.04 KB
/
store.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
package geo
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/goccy/go-json"
"github.com/vikpe/serverstat/qutil"
)
type Store map[string]Location
func NewStoreFromUrl(geoDataUrl string) Store {
var result Store
err := getJson(geoDataUrl, &result)
if err != nil {
timestamp := time.Now().Format(time.RFC850)
fmt.Println(timestamp, "Unable to create geo store:", err.Error())
return make(Store, 0)
}
return result
}
func (s Store) ByAddress(address string) Location {
hostname := strings.SplitN(address, ":", 2)[0]
return s.ByHostname(hostname)
}
func (s Store) ByHostname(hostname string) Location {
ip := qutil.HostnameToIp(hostname)
return s.ByIp(ip)
}
func (s Store) ByIp(ip string) Location {
if info, ok := s[ip]; ok {
return info
}
return Location{}
}
func getJson(url string, target interface{}) error {
httpClient := &http.Client{Timeout: 3 * time.Second}
response, err := httpClient.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
return json.NewDecoder(response.Body).Decode(target)
}