-
Notifications
You must be signed in to change notification settings - Fork 50
/
http_sync.go
165 lines (137 loc) · 3.38 KB
/
http_sync.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
package http
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"github.com/open-feature/flagd/pkg/logger"
"github.com/open-feature/flagd/pkg/sync"
"golang.org/x/crypto/sha3" //nolint:gosec
)
type Sync struct {
URI string
Client Client
Cron Cron
LastBodySHA string
Logger *logger.Logger
BearerToken string
ready bool
}
// Client defines the behaviour required of a http client
type Client interface {
Do(req *http.Request) (*http.Response, error)
}
// Cron defines the behaviour required of a cron
type Cron interface {
AddFunc(spec string, cmd func()) error
Start()
Stop()
}
func (hs *Sync) ReSync(ctx context.Context, dataSync chan<- sync.DataSync) error {
msg, err := hs.Fetch(ctx)
if err != nil {
return err
}
dataSync <- sync.DataSync{FlagData: msg, Source: hs.URI, Type: sync.ALL}
return nil
}
func (hs *Sync) Init(ctx context.Context) error {
// noop
return nil
}
func (hs *Sync) IsReady() bool {
return hs.ready
}
func (hs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
// Initial fetch
fetch, err := hs.Fetch(ctx)
if err != nil {
return err
}
// Set ready state
hs.ready = true
_ = hs.Cron.AddFunc("*/5 * * * *", func() {
body, err := hs.fetchBodyFromURL(ctx, hs.URI)
if err != nil {
hs.Logger.Error(err.Error())
return
}
if len(body) == 0 {
hs.Logger.Debug("configuration deleted")
} else {
if hs.LastBodySHA == "" {
hs.Logger.Debug("new configuration created")
msg, err := hs.Fetch(ctx)
if err != nil {
hs.Logger.Error(fmt.Sprintf("error fetching: %s", err.Error()))
} else {
dataSync <- sync.DataSync{FlagData: msg, Source: hs.URI, Type: sync.ALL}
}
} else {
currentSHA := hs.generateSha(body)
if hs.LastBodySHA != currentSHA {
hs.Logger.Debug("configuration modified")
msg, err := hs.Fetch(ctx)
if err != nil {
hs.Logger.Error(fmt.Sprintf("error fetching: %s", err.Error()))
} else {
dataSync <- sync.DataSync{FlagData: msg, Source: hs.URI, Type: sync.ALL}
}
}
hs.LastBodySHA = currentSHA
}
}
})
hs.Cron.Start()
dataSync <- sync.DataSync{FlagData: fetch, Source: hs.URI, Type: sync.ALL}
<-ctx.Done()
hs.Cron.Stop()
return nil
}
func (hs *Sync) fetchBodyFromURL(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, bytes.NewBuffer(nil))
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
if hs.BearerToken != "" {
bearer := fmt.Sprintf("Bearer %s", hs.BearerToken)
req.Header.Set("Authorization", bearer)
}
resp, err := hs.Client.Do(req)
if err != nil {
return nil, err
}
defer func() {
err = resp.Body.Close()
if err != nil {
hs.Logger.Debug(fmt.Sprintf("error closing the response body: %s", err.Error()))
}
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (hs *Sync) generateSha(body []byte) string {
hasher := sha3.New256()
hasher.Write(body)
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
func (hs *Sync) Fetch(ctx context.Context) (string, error) {
if hs.URI == "" {
return "", errors.New("no HTTP URL string set")
}
body, err := hs.fetchBodyFromURL(ctx, hs.URI)
if err != nil {
return "", err
}
if len(body) != 0 {
hs.LastBodySHA = hs.generateSha(body)
}
return string(body), nil
}