-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
185 lines (160 loc) · 4.17 KB
/
main.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
package main
import (
"crypto/rand"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
)
// DatafileURLTemplate is used to construct the endpoint for retrieving regular datafile from the CDN
const DatafileURLTemplate = "https://cdn.optimizely.com/datafiles/%s.json"
var Datafile []byte
func generateRandomString(length int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
randomBytes := make([]byte, length)
_, err := rand.Read(randomBytes)
if err != nil {
return "", nil
}
for i := 0; i < length; i++ {
randomBytes[i] = charset[randomBytes[i]%byte(len(charset))]
}
return string(randomBytes), nil
}
func webhook(w http.ResponseWriter, r *http.Request) {
df, err := downloadDatafile()
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
nc, err := nats.Connect(os.Getenv("NATS_URL"))
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
js, err := nc.JetStream()
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := publishDatafile(js, df); err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write([]byte("received updated datafile notification"))
}
func downloadDatafile() ([]byte, error) {
url := fmt.Sprintf(DatafileURLTemplate, os.Getenv("SDK_KEY"))
resp, err := http.Get(url)
if err != nil {
return nil, err
}
datafile, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch datafile, statue code: %d", resp.StatusCode)
}
return datafile, nil
}
func publishDatafile(js nats.JetStreamContext, datafile []byte) error {
natsMsg := &nats.Msg{
Subject: "DATAFILE.PUBLISH",
Data: datafile,
}
_, err := js.PublishMsg(natsMsg)
if err != nil {
return fmt.Errorf("failed to publish datafile, err: %s", err.Error())
}
fmt.Println("successfully published datafile at: ", time.Now().String())
return nil
}
func listenForDatafile(js nats.JetStreamContext) error {
strInfo, err := js.AddStream(&nats.StreamConfig{
Name: "DATAFILE",
Description: "Take order for pizza from customer",
Subjects: []string{"DATAFILE.*"},
})
if err != nil {
return errors.Wrap(err, "failed to add stream")
}
randSuffix, err := generateRandomString(5)
if err != nil {
return errors.Wrap(err, "failed to create consumer durable name")
}
conInfo, err := js.AddConsumer(strInfo.Config.Name, &nats.ConsumerConfig{
Durable: fmt.Sprintf("CONSUMER-%s", randSuffix),
AckPolicy: nats.AckExplicitPolicy,
FilterSubject: "DATAFILE.PUBLISH",
})
if err != nil {
return err
}
sub, err := js.PullSubscribe(conInfo.Config.FilterSubject, conInfo.Name, nats.BindStream(conInfo.Stream))
if err != nil {
return err
}
defer func() {
err := sub.Unsubscribe()
if err != nil {
panic(err)
}
}()
fmt.Println("datafile listener is running")
for {
msgs, err := sub.Fetch(1)
if err == nats.ErrTimeout {
continue
}
if err != nil {
fmt.Errorf("failed to fetch message: %s", err.Error())
continue
}
if len(msgs) == 0 {
continue
}
fmt.Println("fetched updated datafile: ")
err = msgs[0].Ack()
if err != nil {
fmt.Errorf("failed to actknowledge message: %s", err.Error())
continue
}
fmt.Println("Received datafile: ", string(msgs[0].Data))
// update datafile
Datafile = msgs[0].Data
fmt.Println("updated datafile at: ", time.Now().String())
}
}
func main() {
// please make sure nats server is already running
nc, err := nats.Connect(os.Getenv("NATS_URL"))
if err != nil {
panic(err)
}
js, err := nc.JetStream()
if err != nil {
panic(err)
}
go func() {
if err := listenForDatafile(js); err != nil {
panic(err)
}
}()
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/webhook/optimizely", webhook)
fmt.Println("Optimizely Agent server is running")
http.ListenAndServe(":8080", r)
}