| 1 |
package acm |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"time" |
| 6 |
"fmt" |
| 7 |
"errors" |
| 8 |
"io/ioutil" |
| 9 |
"strings" |
| 10 |
"encoding/base64" |
| 11 |
"crypto/sha1" |
| 12 |
"crypto/hmac" |
| 13 |
"strconv" |
| 14 |
"net/url" |
| 15 |
) |
| 16 |
|
| 17 |
type Client struct { |
| 18 |
AccessKey string |
| 19 |
SecretKey string |
| 20 |
EndPoint string |
| 21 |
NameSpace string |
| 22 |
TimeOut int |
| 23 |
LocalDataPath string |
| 24 |
servers map[int]string |
| 25 |
HttpClient *http.Client |
| 26 |
} |
| 27 |
|
| 28 |
func NewClient(options ...func(c *Client)) (*Client, error) { |
| 29 |
client := &Client{ |
| 30 |
TimeOut: 30, |
| 31 |
HttpClient: &http.Client{Timeout: 10 * time.Second}, |
| 32 |
servers: make(map[int]string), |
| 33 |
} |
| 34 |
|
| 35 |
for _, option := range options { |
| 36 |
option(client) |
| 37 |
} |
| 38 |
|
| 39 |
err := client.initServer() |
| 40 |
return client, err |
| 41 |
} |
| 42 |
|
| 43 |
func (c *Client) initServer() error { |
| 44 |
if c.EndPoint == "" { |
| 45 |
return errors.New("endpoint not empty") |
| 46 |
} |
| 47 |
|
| 48 |
resp, err := c.HttpClient.Get(fmt.Sprintf("http://%s:8080/diamond-server/diamond", c.EndPoint)) |
| 49 |
|
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
defer resp.Body.Close() |
| 54 |
|
| 55 |
byt, err := ioutil.ReadAll(resp.Body) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
body := strings.TrimSpace(string(byt)) |
| 61 |
|
| 62 |
if resp.StatusCode != 200 { |
| 63 |
return errors.New(body) |
| 64 |
} |
| 65 |
|
| 66 |
servers := strings.Split(body, "\n") |
| 67 |
|
| 68 |
for k, v := range servers { |
| 69 |
if strings.Index(v, ":") == -1 { |
| 70 |
c.servers[k] = v + ":8080" |
| 71 |
} else { |
| 72 |
c.servers[k] = v |
| 73 |
} |
| 74 |
} |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func (c *Client) getServer() string { |
| 80 |
for _, v := range c.servers { |
| 81 |
return v |
| 82 |
} |
| 83 |
return "" |
| 84 |
} |
| 85 |
|
| 86 |
func (c *Client) getSign(params []string) string { |
| 87 |
signStr := strings.Join(params, "+") |
| 88 |
hc := hmac.New(sha1.New, []byte(c.SecretKey)) |
| 89 |
hc.Write([]byte(signStr)) |
| 90 |
return base64.StdEncoding.EncodeToString(hc.Sum(nil)) |
| 91 |
} |
| 92 |
|
| 93 |
func (c *Client) callApi(api string, params map[string]string, method string) (string, error) { |
| 94 |
server := c.getServer() |
| 95 |
|
| 96 |
if server == "" { |
| 97 |
return "", errors.New("get server error") |
| 98 |
} |
| 99 |
|
| 100 |
timeStamp := strconv.FormatInt(time.Now().UnixNano(), 10) |
| 101 |
timeStamp = timeStamp[:13] |
| 102 |
|
| 103 |
spec := "?" |
| 104 |
if strings.Index(api, "?") != -1 { |
| 105 |
spec = "&" |
| 106 |
} |
| 107 |
|
| 108 |
var request *http.Request |
| 109 |
var err error |
| 110 |
query := url.Values{} |
| 111 |
for k, v := range params { |
| 112 |
query.Add(k, v) |
| 113 |
} |
| 114 |
|
| 115 |
if method == "GET" { |
| 116 |
u := fmt.Sprintf("http://%s/%s%s%s", server, api, spec, query.Encode()) |
| 117 |
request, err = http.NewRequest(method, u, nil) |
| 118 |
} else { |
| 119 |
u := fmt.Sprintf("http://%s/%s", server, api) |
| 120 |
request, err = http.NewRequest(method, u, strings.NewReader(query.Encode())) |
| 121 |
} |
| 122 |
|
| 123 |
if err != nil { |
| 124 |
return "", err |
| 125 |
} |
| 126 |
|
| 127 |
request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") |
| 128 |
request.Header.Add("Spas-AccessKey", c.AccessKey) |
| 129 |
request.Header.Add("timeStamp", timeStamp) |
| 130 |
|
| 131 |
if probe, has := params["Probe-Modify-Request"]; has { |
| 132 |
request.Header.Add("longPullingTimeout", strconv.Itoa(c.TimeOut*1000)) |
| 133 |
request.Header.Add("Spas-Signature", c.getSign([]string{probe})) |
| 134 |
c.HttpClient.Timeout = time.Duration(c.TimeOut+30) * time.Second |
| 135 |
} else { |
| 136 |
request.Header.Add("Spas-Signature", c.getSign([]string{c.NameSpace, params["group"], timeStamp})) |
| 137 |
} |
| 138 |
|
| 139 |
resp, err := c.HttpClient.Do(request) |
| 140 |
|
| 141 |
if err != nil { |
| 142 |
return "", err |
| 143 |
} |
| 144 |
defer resp.Body.Close() |
| 145 |
|
| 146 |
byt, err := ioutil.ReadAll(resp.Body) |
| 147 |
if err != nil { |
| 148 |
return "", err |
| 149 |
} |
| 150 |
|
| 151 |
byt, err = GbkToUtf8(byt) |
| 152 |
if err != nil { |
| 153 |
return "", err |
| 154 |
} |
| 155 |
|
| 156 |
body := string(byt) |
| 157 |
|
| 158 |
if resp.StatusCode != 200 { |
| 159 |
return "", errors.New("response error:" + body) |
| 160 |
} |
| 161 |
return body, nil |
| 162 |
} |
| 163 |
|
| 164 |
func (c *Client) GetServers() map[int]string { |
| 165 |
return c.servers |
| 166 |
} |
| 167 |
|
| 168 |
func (c *Client) GetConfig(dataId, group string) (string, error) { |
| 169 |
return c.callApi("diamond-server/config.co", map[string]string{ |
| 170 |
"tenant": c.NameSpace, |
| 171 |
"dataId": dataId, |
| 172 |
"group": group, |
| 173 |
}, "GET") |
| 174 |
} |
| 175 |
|
| 176 |
func (c *Client) GetAllConfigs(pageNo, pageSize int) (string, error) { |
| 177 |
return c.callApi("diamond-server/basestone.do?method=getAllConfigByTenant", map[string]string{ |
| 178 |
"pageNo": strconv.Itoa(pageNo), |
| 179 |
"pageSize": strconv.Itoa(pageSize), |
| 180 |
}, "GET") |
| 181 |
} |
| 182 |
|
| 183 |
func (c *Client) Publish(dataId, group, content string) (string, error) { |
| 184 |
bt, err := Utf8ToGbk([]byte(content)) |
| 185 |
if err != nil { |
| 186 |
return "", err |
| 187 |
} |
| 188 |
|
| 189 |
return c.callApi("diamond-server/basestone.do?method=syncUpdateAll", map[string]string{ |
| 190 |
"tenant": c.NameSpace, |
| 191 |
"dataId": dataId, |
| 192 |
"group": group, |
| 193 |
"content": string(bt), |
| 194 |
}, "POST") |
| 195 |
} |
| 196 |
|
| 197 |
func (c *Client) Subscribe(dataId, group, contentMd5 string) (string, error) { |
| 198 |
probe := strings.Join([]string{dataId, group, contentMd5, c.NameSpace}, "\x02") + "\x01" |
| 199 |
return c.callApi("diamond-server/config.co", map[string]string{ |
| 200 |
"Probe-Modify-Request": probe, |
| 201 |
}, "POST") |
| 202 |
} |
| 203 |
|
| 204 |
func (c *Client) Delete(dateId, group string) (string, error) { |
| 205 |
return c.callApi("diamond-server/datum.do?method=deleteAllDatums", map[string]string{ |
| 206 |
"tenant": c.NameSpace, |
| 207 |
"dataId": dateId, |
| 208 |
"group": group, |
| 209 |
}, "POST") |
| 210 |
} |