From a17e9b6b02ebef216f4f5a64247736063b802b14 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 15 Oct 2015 23:34:54 -0600 Subject: [PATCH] Add ServerBlockIndex and ServerBlockHosts to Controller This way, Setup functions have access to the list of hosts that share the server block, and also, if needed for some reason, the index of the server block in the input --- config/config.go | 4 +++- config/parse/parsing.go | 12 ++++++++++++ config/setup/controller.go | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index b47acf8e..d013d5a5 100644 --- a/config/config.go +++ b/config/config.go @@ -44,7 +44,7 @@ func Load(filename string, input io.Reader) (Group, error) { // Each server block represents similar hosts/addresses. // Iterate each server block and make a config for each one, // executing the directives that were parsed. - for _, sb := range serverBlocks { + for i, sb := range serverBlocks { onces := makeOnces() for _, addr := range sb.Addresses { @@ -75,6 +75,8 @@ func Load(filename string, input io.Reader) (Group, error) { }) return err }, + ServerBlockIndex: i, + ServerBlockHosts: sb.HostList(), } midware, err := dir.setup(controller) diff --git a/config/parse/parsing.go b/config/parse/parsing.go index 6ec04dce..59455391 100644 --- a/config/parse/parsing.go +++ b/config/parse/parsing.go @@ -312,3 +312,15 @@ type ( Host, Port string } ) + +// HostList converts the list of addresses (hosts) +// that are associated with this server block into +// a slice of strings. Each string is a host:port +// combination. +func (sb serverBlock) HostList() []string { + sbHosts := make([]string, len(sb.Addresses)) + for j, addr := range sb.Addresses { + sbHosts[j] = net.JoinHostPort(addr.Host, addr.Port) + } + return sbHosts +} diff --git a/config/setup/controller.go b/config/setup/controller.go index d0b0ee89..eb9b90cf 100644 --- a/config/setup/controller.go +++ b/config/setup/controller.go @@ -24,6 +24,15 @@ type Controller struct { // (not deferred) and may return an error which is // returned by OncePerServerBlock. OncePerServerBlock func(f func() error) error + + // ServerBlockIndex is the 0-based index of the + // server block as it appeared in the input. + ServerBlockIndex int + + // ServerBlockHosts is a list of hosts that are + // associated with this server block. All these + // hosts, consequently, share the same tokens. + ServerBlockHosts []string } // NewTestController creates a new *Controller for