-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
109 lines (100 loc) · 3.29 KB
/
server.go
File metadata and controls
109 lines (100 loc) · 3.29 KB
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
package apollo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"time"
"github.com/apollo-client/apollo-go/log"
"github.com/apollo-client/apollo-go/transport"
"github.com/apollo-client/apollo-go/util"
)
type Apollo struct {
AppId string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
ReleaseKey string `json:"releaseKey"`
Configurations map[string]json.RawMessage `json:"configurations"`
}
type Notifcation struct {
NamespaceName string `json:"namespaceName"`
NotifcationID int64 `json:"notificationId"`
}
func configsURL(app *Application, namespace string, releaseKey string) string {
return fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=%s&ip=%s",
app.Addr,
url.QueryEscape(app.AppId),
url.QueryEscape(app.Cluster),
url.QueryEscape(namespace),
url.QueryEscape(releaseKey),
util.GetLocalAddr(),
)
}
func (c *Client) getConfigs(namespace string, releaseKey string) (int, Apollo, error) {
var apol Apollo
app := c.App
reqURL := configsURL(app, namespace, releaseKey)
header := c.opts.Auth.Header(reqURL, app.AppId, app.Secret)
var body []byte
status, body, err := c.opts.Transport.Do(reqURL, transport.WithHeaders(header))
if err != nil {
log.Errorf("get configs namespace: %s, release:%s, err: %v\n", namespace, releaseKey, err)
}
// new body must write to backup
if c.opts.EnableBackup && err == nil && status == http.StatusOK && body != nil {
filePath := path.Join(c.opts.BackupPath, fmt.Sprintf("%s-%s-%s", c.App.AppId, c.App.Cluster, namespace))
_ = c.opts.Backup.Write(filePath, body)
}
// request failed, read from backup
if c.opts.EnableBackup && (err != nil || status != http.StatusOK) {
filePath := path.Join(c.opts.BackupPath, fmt.Sprintf("%s-%s-%s", c.App.AppId, c.App.Cluster, namespace))
body, err = c.opts.Backup.Read(filePath)
// read success, set status ok
if err == nil && body != nil {
status = http.StatusOK
}
}
if err != nil {
log.Errorf("get configs namespace: %s, release:%s, err: %v\n", namespace, releaseKey, err)
return status, apol, err
}
if status != http.StatusOK {
return status, apol, err
}
err = json.Unmarshal(body, &apol)
if err != nil {
log.Errorf("get configs namespace: %s, release:%s, err: %v\n", namespace, releaseKey, err)
return status, apol, err
}
return status, apol, nil
}
func notificationURL(app *Application, ns []*Notifcation) string {
bs, _ := json.Marshal(ns)
return fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s¬ifications=%s",
app.Addr,
url.QueryEscape(app.AppId),
url.QueryEscape(app.Cluster),
url.QueryEscape(string(bs)))
}
func (c *Client) getNotifications(ns []*Notifcation) (int, []*Notifcation, error) {
app := c.App
reqURL := notificationURL(app, ns)
header := c.opts.Auth.Header(reqURL, app.AppId, app.Secret)
var body []byte
status, body, err := c.opts.Transport.Do(reqURL, transport.WithHeaders(header), transport.WithTimeout(10*time.Minute))
if err != nil {
log.Errorf("get notifications url: %s, err: %v\n", reqURL, err)
return status, nil, err
}
if status != http.StatusOK {
return status, nil, err
}
res := make([]*Notifcation, 0)
err = json.Unmarshal(body, &res)
if err != nil {
log.Errorf("get notifications url: %s, err: %v\n", reqURL, err)
return status, res, err
}
return status, res, nil
}