forked from philchia/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
70 lines (60 loc) · 1.42 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
package agollo
import (
"fmt"
"net"
"net/url"
"strings"
)
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, releaseKey string) string {
var addr = conf.MetaAddr
return fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=%s&ip=%s",
httpurl(addr),
url.QueryEscape(conf.AppID),
url.QueryEscape(conf.Cluster),
url.QueryEscape(nomalizeNamespace(namespace)),
releaseKey,
getLocalIP())
}
func strIn(slice []string, target string) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}