This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchservices.go
86 lines (76 loc) · 1.95 KB
/
watchservices.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
package caddyconsul
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/consul/api"
)
type service struct {
Name string
Address string
Port int
}
func (s *caddyfile) WatchServices(reload bool) {
opts := api.QueryOptions{
WaitIndex: s.lastService,
WaitTime: 5 * time.Minute,
}
if !reload {
opts.WaitTime = time.Second
}
fmt.Println("Watching for new service with index", s.lastService, "or better")
// TODO
services, meta, err := catalog.Services(&opts)
if err != nil {
// TODO should probably handle this better
return
}
if meta.LastIndex > s.lastService {
s.lastService = meta.LastIndex
}
myservices := make(map[string]map[string][]*service)
for servicename := range services {
//fmt.Println("Service:", servicename)
// Get all instances for this service
instances, _, _ := catalog.Service(servicename, "", nil)
// TODO should probably check error
for _, instance := range instances {
for _, tag := range instance.ServiceTags {
if !strings.HasPrefix(tag, "urlprefix-") {
continue
}
cleantag := strings.TrimPrefix(tag, "urlprefix-")
keybits := strings.SplitN(cleantag, "/", 2)
if len(keybits) < 2 {
// Our urlprefix isn't long enough, needs at least one forward slash
continue
}
// Add the / back
keybits[1] = "/" + keybits[1]
myservice := &service{
Name: instance.ServiceName,
Address: instance.Address,
Port: instance.ServicePort,
}
// TODO handle subdomains
if instance.ServiceAddress != "" {
myservice.Address = instance.ServiceAddress
}
if myservices[keybits[0]] == nil {
myservices[keybits[0]] = make(map[string][]*service)
}
myservices[keybits[0]][keybits[1]] = append(myservices[keybits[0]][keybits[1]], myservice)
if s.domains[keybits[0]] == nil {
s.domains[keybits[0]] = &domain{
Config: "",
}
}
}
}
}
s.services = myservices
s.buildConfig()
if reload {
reloadCaddy()
}
}