/
config.go
103 lines (89 loc) · 2.45 KB
/
config.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
package server
import (
"bytes"
"context"
"strings"
"text/template"
"time"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
api "github.com/stellarproject/radiant/api/v1"
)
const (
configTemplate = `# radiant proxy config
*:{{ $.HTTPPort }} {
status 200 /healthz
}
{{ range $server := .Servers }}
{{ if $server.TLS }}https{{ else }}http{{ end }}://{{ $server.Host }}:{{ if $server.TLS }}{{ $.HTTPSPort }}{{ else }}{{ $.HTTPPort }}{{ end }} {
proxy {{ $server.Path }} { {{ if ne $server.Preset "" }}
{{ $server.Preset }}{{ end }}
policy {{policyname $server.Policy }} {{ if $t := $server.ProxyTryDuration }}
try_duration {{duration $t }}{{ end }} {{ if $t := $server.ProxyFailTimeout }}
fail_timeout {{duration $t }}{{ end }} {{if $server.ProxyUpstreamHeaders}}
{{ range $k, $v := $server.ProxyUpstreamHeaders }}header_upstream {{ $k }} "{{ $v }}"
{{ end }}{{ end }}
{{ range $upstream := $server.Upstreams }}upstream {{ $upstream }}
{{ end }}
}
{{ with $t := $server.Timeouts }}{{ if $t }}timeouts {{duration $t }} {{ end }} {{ end }}
}{{ end }}
`
)
type proxyConfig struct {
HTTPPort int
HTTPSPort int
Servers []*api.Server
}
func (s *Server) Config(ctx context.Context, req *api.ConfigRequest) (*api.ConfigResponse, error) {
cf := s.instance.Caddyfile()
return &api.ConfigResponse{
Data: cf.Body(),
}, nil
}
func policyName(v api.Policy) string {
return strings.ToLower(v.String())
}
func duration(v *types.Duration) time.Duration {
d, err := types.DurationFromProto(v)
if err != nil {
logrus.Error(errors.Wrap(err, "error converting proto duration"))
}
return d
}
func (s *Server) generateServerConfig() ([]byte, error) {
servers, err := s.datastore.Servers()
if err != nil {
return nil, err
}
srvs := []*api.Server{}
for _, srv := range servers {
srvs = append(srvs, srv)
}
b, err := generateConfig(s.config.HTTPPort, s.config.HTTPSPort, srvs)
if err != nil {
return nil, err
}
return b, nil
}
func generateConfig(httpPort, httpsPort int, servers []*api.Server) ([]byte, error) {
t := template.New("radiant").Funcs(template.FuncMap{
"policyname": policyName,
"duration": duration,
})
tmpl, err := t.Parse(configTemplate)
if err != nil {
return nil, err
}
config := &proxyConfig{
HTTPPort: httpPort,
HTTPSPort: httpsPort,
Servers: servers,
}
var c bytes.Buffer
if err := tmpl.Execute(&c, config); err != nil {
return nil, err
}
return c.Bytes(), nil
}