-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcommon.go
90 lines (78 loc) · 1.99 KB
/
common.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package agollo
import (
"encoding/json"
"fmt"
"net"
"net/url"
"strings"
)
type messages struct {
Details map[string]interface{} `json:"details,omitempty"`
}
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, a := range addrs {
if ip4 := toIP4(a); ip4 != nil {
return ip4.String()
}
}
return ""
}
func toIP4(addr net.Addr) net.IP {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
return ipnet.IP.To4()
}
return nil
}
func httpurl(ipOrAddr string) string {
if strings.HasPrefix(ipOrAddr, "http://") || strings.HasPrefix(ipOrAddr, "https://") {
return ipOrAddr
}
return fmt.Sprintf("http://%s", ipOrAddr)
}
func nomalizeNamespace(namespace string) string {
return strings.TrimSuffix(namespace, propertiesSuffix)
}
func notificationURL(conf *Conf, notifications string) string {
var addr = conf.MetaAddr
return fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s¬ifications=%s",
httpurl(addr),
url.QueryEscape(conf.AppID),
url.QueryEscape(conf.Cluster),
url.QueryEscape(notifications))
}
func configURL(conf *Conf, namespace string, notificationId int) string {
var addr = conf.MetaAddr
message := getMessages(conf, namespace, notificationId)
return fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=&ip=%s&messages=%s",
httpurl(addr),
url.QueryEscape(conf.AppID),
url.QueryEscape(conf.Cluster),
url.QueryEscape(nomalizeNamespace(namespace)),
getLocalIP(),
url.QueryEscape(message))
}
func getMessages(conf *Conf, namespace string, notificationId int) string {
key := fmt.Sprintf("%s+%s+%s", conf.AppID, conf.Cluster, nomalizeNamespace(namespace))
d := make(map[string]interface{})
d[key] = notificationId
m := &messages{}
m.Details = d
message, err := json.Marshal(m)
if err != nil {
fmt.Println("get messaget err:", err)
return ""
}
return string(message)
}
func strIn(slice []string, target string) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}