-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchkv.go
53 lines (46 loc) · 966 Bytes
/
watchkv.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
package caddyconsul
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/consul/api"
)
type domain struct {
Config string
}
func (s *caddyfile) WatchKV(reload bool) {
opts := api.QueryOptions{
WaitIndex: s.lastKV,
WaitTime: 5 * time.Minute,
}
if !reload {
opts.WaitTime = time.Second
}
fmt.Println("Watching for new KV with index", s.lastKV, "or better")
pairs, meta, err := kv.List("caddy/", &opts)
if err != nil {
return
}
if meta.LastIndex > s.lastKV {
s.lastKV = meta.LastIndex
}
// If there's nothing, at least put our KV value so the user isn't lost
if len(pairs) == 0 {
kv.Put(&api.KVPair{Key: "caddy/"}, nil)
}
domains := make(map[string]*domain)
for _, k := range pairs {
keybits := strings.SplitN(k.Key, "/", 3)
if len(keybits) != 2 || keybits[1] == "" {
continue
}
domains[keybits[1]] = &domain{
Config: string(k.Value),
}
}
s.domains = domains
s.buildConfig()
if reload {
reloadCaddy()
}
}