-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetcher.go
executable file
·187 lines (167 loc) · 4.57 KB
/
fetcher.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @Author: llh
* @Date: 2018-01-25 16:08:29
* @Last Modified by: llh
*/
package disconf_client
import (
"github.com/parnurzeal/gorequest"
"time"
"os"
"io/ioutil"
"fmt"
"strings"
"net/http"
)
type IFetcher interface {
getValue(suffixUrl string) (string, []error)
downloadFile(suffixUrl, fileName string) []error
getAllConf(suffixUrl string) ([]*Result, []error)
getZkHost() (string, []error)
}
type Fetcher struct {
//文件下载目录
downloadDir string
// 获取远程配置 重试次数
retryTime int
// 获取远程配置 重试时休眠时间 (s)
retrySleepSeconds int
// host List
hostList []string
}
type zooHostsResp struct {
Status int `json:"status"`
Message string `json:"message"`
Value string `json:"value"`
}
type confListResp struct {
Success string `json:"success"`
Message interface{} `json:"message"`
Page struct {
Results []*Result `json:"result"`
} `json:"page"`
}
type Result struct {
Id int `json:"id"`
Genre int `json:"type"`
Status int `json:"status"`
Name string `json:"name"`
Value string `json:"value"`
AppId int `json:"appId"`
Version string `json:"version"`
EnvId int `json:"envId"`
}
const (
EMPTY_STRING = ""
PREFIX_HTTP = "http://"
PREFIX_HTTPS = "https://"
DISCONF_STORE_ACTION = "/api/config/list"
DISCONF_ITEM_ACTION = "/api/config/item"
DISCONF_FILE_ACTION = "/api/config/file"
DISCONF_ZOO_HOSTS_ACTION = "/api/zoo/hosts"
STRING_TRUE = "true"
ZOO_SUCCESS_STATUS = 1
)
type itemResp struct {
Status int `json:"status"`
Message string `json:"message"`
Value string `json:"value"`
}
func (f Fetcher) getValue(suffixUrl string) (string, []error) {
urls := f.getUrls(DISCONF_ITEM_ACTION + suffixUrl)
var resp itemResp
errs := []error{}
for _, url := range urls {
_, _, httpErrs := gorequest.New().Get(url).
Retry(f.retryTime, time.Duration(f.retrySleepSeconds)*time.Second, http.StatusBadRequest, http.StatusInternalServerError).
EndStruct(&resp)
if len(httpErrs) <= 0 {
return resp.Value, nil
}
errs = append(errs, httpErrs...)
}
return EMPTY_STRING, errs
}
func (f Fetcher) downloadFile(suffixUrl, fileName string) []error {
errs := []error{}
_, err := os.Stat(f.downloadDir)
if err != nil {
err = os.MkdirAll(f.downloadDir, 0777)
if err != nil {
return append(errs, err)
}
}
bodyBytes, errs := f.httpEndByte(DISCONF_FILE_ACTION + suffixUrl)
if len(errs) > 0 {
return errs
}
if err = ioutil.WriteFile(f.downloadDir+fileName, bodyBytes, os.ModeAppend); err != nil {
return append(errs, err)
}
return nil
}
func (f Fetcher) getAllConf(suffixUrl string) ([]*Result, []error) {
urls := f.getUrls(DISCONF_STORE_ACTION + suffixUrl)
var resp confListResp
errs := []error{}
for _, url := range urls {
_, _, httpErrs := gorequest.New().Get(url).
Retry(f.retryTime, time.Duration(f.retrySleepSeconds)*time.Second, http.StatusBadRequest, http.StatusInternalServerError).
EndStruct(&resp)
if len(httpErrs) <= 0 {
if resp.Success != STRING_TRUE {
errs = append(errs, fmt.Errorf("get all conf %v", resp.Message))
continue
}
return resp.Page.Results, nil
}
errs = append(errs, httpErrs...)
}
return nil, errs
}
func (f Fetcher) getZkHost() (string, []error) {
urls := f.getUrls(DISCONF_ZOO_HOSTS_ACTION)
var resp zooHostsResp
errs := []error{}
for _, url := range urls {
_, _, httpErrs := gorequest.New().Get(url).
Retry(f.retryTime, time.Duration(f.retrySleepSeconds)*time.Second, http.StatusBadRequest, http.StatusInternalServerError).
EndStruct(&resp)
if len(httpErrs) <= 0 {
if resp.Status != ZOO_SUCCESS_STATUS {
errs = append(errs, fmt.Errorf("get zoo hosts [err:%v]", resp.Message))
continue
}
return resp.Value, nil
}
errs = append(errs, httpErrs...)
}
return EMPTY_STRING, errs
}
func (f Fetcher) httpEndByte(suffixUrl string) ([]byte, []error) {
errs := []error{}
urls := f.getUrls(suffixUrl)
for _, url := range urls {
_, bodyBytes, httpErrs := gorequest.New().Get(url).
Retry(f.retryTime, time.Duration(f.retrySleepSeconds)*time.Second, http.StatusBadRequest, http.StatusInternalServerError).
EndBytes()
if len(httpErrs) <= 0 {
return bodyBytes, nil
}
errs = append(errs, httpErrs...)
}
return nil, errs
}
func (f Fetcher) getUrls(suffixUrl string) []string {
urls := []string{}
for _, host := range f.hostList {
if !strings.HasPrefix(host, PREFIX_HTTP) {
if !strings.HasPrefix(host, PREFIX_HTTPS) {
host = PREFIX_HTTP + host
}
}
host += suffixUrl
urls = append(urls, host)
}
return urls
}