-
Notifications
You must be signed in to change notification settings - Fork 1
/
caddyfile.go
58 lines (49 loc) · 958 Bytes
/
caddyfile.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
package caddyconsul
import (
"bytes"
"fmt"
"text/template"
"time"
"github.com/hashicorp/consul/api"
)
var consulGenerator *caddyfile
type caddyfile struct {
contents string
lastKV uint64
lastService uint64
domains map[string]*domain
services map[string][]*api.CatalogService
}
func (s *caddyfile) Body() []byte {
fmt.Println("Generated config:")
fmt.Println(s.contents)
return []byte(s.contents)
}
func (s *caddyfile) Path() string {
return ""
}
func (s *caddyfile) ServerType() string {
return "http"
}
func (s *caddyfile) StartWatching() {
go func() {
for {
time.Sleep(1 * time.Second)
s.WatchServices(true)
}
}()
}
func (s *caddyfile) buildConfig() {
t := template.New("Caddyfile.tmpl")
var err error
t, err = t.ParseFiles(caddyfilePath)
if err != nil {
s.contents = ""
return
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, s.services); err != nil {
return
}
s.contents = tpl.String()
}