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

This commit is contained in:
John Belamaric 2018-01-27 17:25:39 -05:00 committed by GitHub
parent 3b4235a7c6
commit d27be86e3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,6 +28,18 @@ 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() {
u, err := NewStaticUpstream(c)
if err != nil {
return upstreams, err
}
upstreams = append(upstreams, u)
}
return upstreams, nil
}
// NewStaticUpstream parses the configuration of a single upstream
// starting from the FROM
func NewStaticUpstream(c *caddyfile.Dispenser) (Upstream, error) {
upstream := &staticUpstream{ upstream := &staticUpstream{
from: ".", from: ".",
HealthCheck: healthcheck.HealthCheck{ HealthCheck: healthcheck.HealthCheck{
@ -38,28 +50,28 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
} }
if !c.Args(&upstream.from) { if !c.Args(&upstream.from) {
return upstreams, c.ArgErr() return upstream, c.ArgErr()
} }
upstream.from = plugin.Host(upstream.from).Normalize() upstream.from = plugin.Host(upstream.from).Normalize()
to := c.RemainingArgs() to := c.RemainingArgs()
if len(to) == 0 { if len(to) == 0 {
return upstreams, c.ArgErr() return upstream, c.ArgErr()
} }
// process the host list, substituting in any nameservers in files // process the host list, substituting in any nameservers in files
toHosts, err := dnsutil.ParseHostPortOrFile(to...) toHosts, err := dnsutil.ParseHostPortOrFile(to...)
if err != nil { if err != nil {
return upstreams, err return upstream, err
} }
if len(toHosts) > max { if len(toHosts) > max {
return upstreams, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts)) return upstream, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts))
} }
for c.NextBlock() { for c.NextBlock() {
if err := parseBlock(c, upstream); err != nil { if err := parseBlock(c, upstream); err != nil {
return upstreams, err return upstream, err
} }
} }
@ -75,9 +87,7 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
} }
upstream.Start() upstream.Start()
upstreams = append(upstreams, upstream) return upstream, nil
}
return upstreams, nil
} }
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error { func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {