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,6 +28,18 @@ type staticUpstream struct {
func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
var upstreams []Upstream
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{
from: ".",
HealthCheck: healthcheck.HealthCheck{
......@@ -38,28 +50,28 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
}
if !c.Args(&upstream.from) {
return upstreams, c.ArgErr()
return upstream, c.ArgErr()
}
upstream.from = plugin.Host(upstream.from).Normalize()
to := c.RemainingArgs()
if len(to) == 0 {
return upstreams, c.ArgErr()
return upstream, c.ArgErr()
}
// process the host list, substituting in any nameservers in files
toHosts, err := dnsutil.ParseHostPortOrFile(to...)
if err != nil {
return upstreams, err
return upstream, err
}
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() {
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()
upstreams = append(upstreams, upstream)
}
return upstreams, nil
return upstream, nil
}
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