-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcheck.go
126 lines (99 loc) · 2.97 KB
/
check.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"fmt"
"github.com/apolloconfig/agollo/v4/env/config"
"net/http"
"strings"
"github.com/apolloconfig/agollo/v4"
)
var namespaces = make(map[string]*struct{}, 0)
var appConfig = &config.AppConfig{
AppID: "agollo-test",
Cluster: "dev",
IP: "http://81.68.181.139:8080",
NamespaceName: "testjson.json,testyml.yml",
IsBackupConfig: false,
}
var client agollo.Client
func main() {
var err error
agollo.SetLogger(&DefaultLogger{})
client, err = agollo.StartWithConfig(func() (*config.AppConfig, error) {
return appConfig, nil
})
fmt.Println("err:", err)
http.HandleFunc("/check", GetAllConfig)
http.ListenAndServe("0.0.0.0:9000", nil)
}
func GetAllConfig(rw http.ResponseWriter, req *http.Request) {
ns := strings.Split(appConfig.NamespaceName, ",")
for _, n := range ns {
namespaces[n] = &struct{}{}
}
n := req.URL.Query().Get("namespace")
if n != "" {
namespaces[n] = &struct{}{}
}
arr := make([]string, 0)
for k := range namespaces {
arr = append(arr, k)
}
var namespaceName = strings.Join(arr, ",")
var buffer bytes.Buffer
buffer.WriteString("<html>")
buffer.WriteString("<meta http-equiv=\"refresh\" content=\"3\">")
key := req.URL.Query().Get("key")
if key == "" {
buffer.WriteString(fmt.Sprintf("AppId : %s <br/>", appConfig.AppID))
buffer.WriteString(fmt.Sprintf("Cluster : %s <br/>", appConfig.Cluster))
namespaces := strings.Split(namespaceName, ",")
for _, namespace := range namespaces {
buffer.WriteString(fmt.Sprintf("ReleaseKey : %s <br/>", appConfig.GetCurrentApolloConfig().GetReleaseKey(namespace)))
writeConfig(&buffer, namespace)
}
}
//buffer.WriteString(fmt.Sprintf("NamespaceName : %s <br/>", "testjson.json"))
//buffer.WriteString("Configurations: <br/>")
//cache := agollo.GetConfig("testjson.json")
buffer.WriteString("</html>")
rw.Write(buffer.Bytes())
}
func writeConfig(buffer *bytes.Buffer, namespace string) {
buffer.WriteString(fmt.Sprintf("NamespaceName : %s <br/>", namespace))
buffer.WriteString("Configurations: <br/>")
cache := client.GetConfigCache(namespace)
if cache == nil {
return
}
cache.Range(func(key, value interface{}) bool {
buffer.WriteString(fmt.Sprintf("key : %s , value : %s <br/>", key, value))
return true
})
}
type DefaultLogger struct {
}
func (this *DefaultLogger) Debugf(format string, params ...interface{}) {
this.Debug(format, params)
}
func (this *DefaultLogger) Infof(format string, params ...interface{}) {
this.Debug(format, params)
}
func (this *DefaultLogger) Warnf(format string, params ...interface{}) {
this.Debug(format, params)
}
func (this *DefaultLogger) Errorf(format string, params ...interface{}) {
this.Debug(format, params)
}
func (this *DefaultLogger) Debug(v ...interface{}) {
fmt.Println(v)
}
func (this *DefaultLogger) Info(v ...interface{}) {
this.Debug(v)
}
func (this *DefaultLogger) Warn(v ...interface{}) {
this.Debug(v)
}
func (this *DefaultLogger) Error(v ...interface{}) {
this.Debug(v)
}