Commit d27be86e authored by John Belamaric's avatar John Belamaric Committed by GitHub

Minor refactor of proxy parsing to make upstreams re-usable in other plugins (#1426)

parent 3b4235a7
...@@ -28,56 +28,66 @@ type staticUpstream struct { ...@@ -28,56 +28,66 @@ type staticUpstream struct {
func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) { func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
var upstreams []Upstream var upstreams []Upstream
for c.Next() { for c.Next() {
upstream := &staticUpstream{ u, err := NewStaticUpstream(c)
from: ".", if err != nil {
HealthCheck: healthcheck.HealthCheck{ return upstreams, err
FailTimeout: 5 * time.Second,
MaxFails: 3,
},
ex: newDNSEx(),
} }
upstreams = append(upstreams, u)
}
return upstreams, nil
}
if !c.Args(&upstream.from) { // NewStaticUpstream parses the configuration of a single upstream
return upstreams, c.ArgErr() // starting from the FROM
} func NewStaticUpstream(c *caddyfile.Dispenser) (Upstream, error) {
upstream.from = plugin.Host(upstream.from).Normalize() upstream := &staticUpstream{
from: ".",
HealthCheck: healthcheck.HealthCheck{
FailTimeout: 5 * time.Second,
MaxFails: 3,
},
ex: newDNSEx(),
}
to := c.RemainingArgs() if !c.Args(&upstream.from) {
if len(to) == 0 { return upstream, c.ArgErr()
return upstreams, c.ArgErr() }
} upstream.from = plugin.Host(upstream.from).Normalize()
// process the host list, substituting in any nameservers in files to := c.RemainingArgs()
toHosts, err := dnsutil.ParseHostPortOrFile(to...) if len(to) == 0 {
if err != nil { return upstream, c.ArgErr()
return upstreams, err }
}
if len(toHosts) > max { // process the host list, substituting in any nameservers in files
return upstreams, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts)) toHosts, err := dnsutil.ParseHostPortOrFile(to...)
} if err != nil {
return upstream, err
}
for c.NextBlock() { if len(toHosts) > max {
if err := parseBlock(c, upstream); err != nil { return upstream, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts))
return upstreams, err }
}
for c.NextBlock() {
if err := parseBlock(c, upstream); err != nil {
return upstream, err
} }
}
upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts)) upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts))
for i, host := range toHosts { for i, host := range toHosts {
uh := &healthcheck.UpstreamHost{ uh := &healthcheck.UpstreamHost{
Name: host, Name: host,
FailTimeout: upstream.FailTimeout, FailTimeout: upstream.FailTimeout,
CheckDown: checkDownFunc(upstream), CheckDown: checkDownFunc(upstream),
}
upstream.Hosts[i] = uh
} }
upstream.Start() upstream.Hosts[i] = uh
upstreams = append(upstreams, upstream)
} }
return upstreams, nil upstream.Start()
return upstream, nil
} }
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error { func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment