Minor refactor of proxy parsing to make upstreams re-usable in other plugins (#1426)
This commit is contained in:
parent
3b4235a7c6
commit
d27be86e3e
1 changed files with 55 additions and 45 deletions
|
@ -28,58 +28,68 @@ 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: ".",
|
|
||||||
HealthCheck: healthcheck.HealthCheck{
|
|
||||||
FailTimeout: 5 * time.Second,
|
|
||||||
MaxFails: 3,
|
|
||||||
},
|
|
||||||
ex: newDNSEx(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !c.Args(&upstream.from) {
|
|
||||||
return upstreams, c.ArgErr()
|
|
||||||
}
|
|
||||||
upstream.from = plugin.Host(upstream.from).Normalize()
|
|
||||||
|
|
||||||
to := c.RemainingArgs()
|
|
||||||
if len(to) == 0 {
|
|
||||||
return upstreams, c.ArgErr()
|
|
||||||
}
|
|
||||||
|
|
||||||
// process the host list, substituting in any nameservers in files
|
|
||||||
toHosts, err := dnsutil.ParseHostPortOrFile(to...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return upstreams, err
|
return upstreams, err
|
||||||
}
|
}
|
||||||
|
upstreams = append(upstreams, u)
|
||||||
if len(toHosts) > max {
|
|
||||||
return upstreams, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts))
|
|
||||||
}
|
|
||||||
|
|
||||||
for c.NextBlock() {
|
|
||||||
if err := parseBlock(c, upstream); err != nil {
|
|
||||||
return upstreams, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts))
|
|
||||||
|
|
||||||
for i, host := range toHosts {
|
|
||||||
uh := &healthcheck.UpstreamHost{
|
|
||||||
Name: host,
|
|
||||||
FailTimeout: upstream.FailTimeout,
|
|
||||||
CheckDown: checkDownFunc(upstream),
|
|
||||||
}
|
|
||||||
upstream.Hosts[i] = uh
|
|
||||||
}
|
|
||||||
upstream.Start()
|
|
||||||
|
|
||||||
upstreams = append(upstreams, upstream)
|
|
||||||
}
|
}
|
||||||
return upstreams, nil
|
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{
|
||||||
|
FailTimeout: 5 * time.Second,
|
||||||
|
MaxFails: 3,
|
||||||
|
},
|
||||||
|
ex: newDNSEx(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.Args(&upstream.from) {
|
||||||
|
return upstream, c.ArgErr()
|
||||||
|
}
|
||||||
|
upstream.from = plugin.Host(upstream.from).Normalize()
|
||||||
|
|
||||||
|
to := c.RemainingArgs()
|
||||||
|
if len(to) == 0 {
|
||||||
|
return upstream, c.ArgErr()
|
||||||
|
}
|
||||||
|
|
||||||
|
// process the host list, substituting in any nameservers in files
|
||||||
|
toHosts, err := dnsutil.ParseHostPortOrFile(to...)
|
||||||
|
if err != nil {
|
||||||
|
return upstream, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(toHosts) > max {
|
||||||
|
return upstream, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts))
|
||||||
|
}
|
||||||
|
|
||||||
|
for c.NextBlock() {
|
||||||
|
if err := parseBlock(c, upstream); err != nil {
|
||||||
|
return upstream, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts))
|
||||||
|
|
||||||
|
for i, host := range toHosts {
|
||||||
|
uh := &healthcheck.UpstreamHost{
|
||||||
|
Name: host,
|
||||||
|
FailTimeout: upstream.FailTimeout,
|
||||||
|
CheckDown: checkDownFunc(upstream),
|
||||||
|
}
|
||||||
|
upstream.Hosts[i] = uh
|
||||||
|
}
|
||||||
|
upstream.Start()
|
||||||
|
|
||||||
|
return upstream, nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
|
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
case "policy":
|
case "policy":
|
||||||
|
|
Loading…
Add table
Reference in a new issue