Skip to content

Commit a002565

Browse files
author
liuxingwang
committed
[fix] 兼容没有带任何scheme的地址配置,默认http://
1 parent 1757ab4 commit a002565

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

agollo.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func New(configServerURL, appID string, opts ...Option) (Agollo, error) {
7878
errorsCh: make(chan *LongPollerError),
7979
opts: newOptions(opts...),
8080
}
81-
a.opts.ConfigServerURL = configServerURL
81+
82+
a.opts.ConfigServerURL = normalizeURL(configServerURL)
8283
a.opts.AppID = appID
8384

8485
return a.preload()

apollo_client.go

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func NewApolloClient(opts ...ApolloClientOption) ApolloClient {
107107
}
108108

109109
func (c *apolloClient) Notifications(configServerURL, appID, cluster string, notifications []Notification) (status int, result []Notification, err error) {
110+
configServerURL = normalizeURL(configServerURL)
110111
url := fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s&notifications=%s",
111112
configServerURL,
112113
url.QueryEscape(appID),
@@ -139,6 +140,7 @@ func (c *apolloClient) GetConfigsFromNonCache(configServerURL, appID, cluster, n
139140
opt(&options)
140141
}
141142

143+
configServerURL = normalizeURL(configServerURL)
142144
url := fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=%s&ip=%s",
143145
configServerURL,
144146
url.QueryEscape(appID),
@@ -169,6 +171,7 @@ func (c *apolloClient) GetConfigsFromNonCache(configServerURL, appID, cluster, n
169171
}
170172

171173
func (c *apolloClient) GetConfigsFromCache(configServerURL, appID, cluster, namespace string) (config Configurations, err error) {
174+
configServerURL = normalizeURL(configServerURL)
172175
url := fmt.Sprintf("%s/configfiles/json/%s/%s/%s?ip=%s",
173176
configServerURL,
174177
url.QueryEscape(appID),

util.go

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package agollo
22

3+
import "strings"
4+
35
func stringInSlice(a string, list []string) bool {
46
for _, b := range list {
57
if b == a {
@@ -8,3 +10,11 @@ func stringInSlice(a string, list []string) bool {
810
}
911
return false
1012
}
13+
14+
func normalizeURL(url string) string {
15+
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
16+
url = "http://" + url
17+
}
18+
19+
return strings.TrimSuffix(url, "/")
20+
}

util_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package agollo
2+
3+
import "testing"
4+
5+
func TestNormalizeURL(t *testing.T) {
6+
tests := []struct {
7+
url string
8+
expected string
9+
}{
10+
{
11+
"localhost",
12+
"http://localhost",
13+
},
14+
{
15+
"http://localhost",
16+
"http://localhost",
17+
},
18+
{
19+
"https://localhost",
20+
"https://localhost",
21+
},
22+
}
23+
24+
for i, test := range tests {
25+
t.Logf("run test (%v): %v", i, test.url)
26+
27+
actual := normalizeURL(test.url)
28+
if actual != test.expected {
29+
t.Errorf(" should be equal (expected=%v, actual=%v)", test.expected, actual)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)