-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-app.go
80 lines (64 loc) · 1.27 KB
/
simple-app.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sync/atomic"
"time"
"github.com/coreos/etcd/clientv3"
)
var (
discount atomic.Value
)
func handler(w http.ResponseWriter, r *http.Request) {
price := 50.0
fmt.Fprintf(w, "Temos o produto x\n")
switch discount.Load().(string) {
case "blackfriday":
price = price * 0.9
case "pre-blackfriday":
price = price * 1.3
}
fmt.Fprintf(w, "Preço %0.2f", price)
}
func main() {
go loadConfig()
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "9000"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func loadConfig() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
log.Fatal(err)
}
defer cli.Close()
key := "discount"
resp, err := cli.Get(context.Background(), key)
if err != nil {
log.Fatal(err)
}
if len(resp.Kvs) == 0 {
log.Fatalf("%s is not defined", key)
}
discount.Store(string(resp.Kvs[0].Value))
watcher := cli.Watch(
context.Background(),
key,
clientv3.WithRev(resp.Header.Revision),
)
for resp := range watcher {
for _, ev := range resp.Events {
log.Printf("Discount now is %s", ev.Kv.Value)
discount.Store(string(ev.Kv.Value))
}
}
}