From fe7ad8ee056c47b1d52d890cdb70fa6ecba0a38b Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 17 Oct 2015 14:11:32 -0600 Subject: [PATCH 1/2] core: Controller has field to persist server state Also added ServerBlockHostIndex --- config/config.go | 21 ++++++++++++++++++--- config/setup/controller.go | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index d013d5a5..1e61cd9e 100644 --- a/config/config.go +++ b/config/config.go @@ -46,8 +46,9 @@ func Load(filename string, input io.Reader) (Group, error) { // executing the directives that were parsed. for i, sb := range serverBlocks { onces := makeOnces() + storages := makeStorages() - for _, addr := range sb.Addresses { + for j, addr := range sb.Addresses { config := server.Config{ Host: addr.Host, Port: addr.Port, @@ -75,8 +76,10 @@ func Load(filename string, input io.Reader) (Group, error) { }) return err }, - ServerBlockIndex: i, - ServerBlockHosts: sb.HostList(), + ServerBlockIndex: i, + ServerBlockHostIndex: j, + ServerBlockHosts: sb.HostList(), + ServerBlockStorage: storages[dir.name], } midware, err := dir.setup(controller) @@ -121,6 +124,18 @@ func makeOnces() map[string]*sync.Once { return onces } +// makeStorages makes a map of directive name to interface{} +// so that directives' setup functions can persist state +// between different hosts on the same server block during the +// setup phase. +func makeStorages() map[string]interface{} { + storages := make(map[string]interface{}) + for _, dir := range directiveOrder { + storages[dir.name] = nil + } + return storages +} + // arrangeBindings groups configurations by their bind address. For example, // a server that should listen on localhost and another on 127.0.0.1 will // be grouped into the same address: 127.0.0.1. It will return an error diff --git a/config/setup/controller.go b/config/setup/controller.go index eb9b90cf..04873082 100644 --- a/config/setup/controller.go +++ b/config/setup/controller.go @@ -29,17 +29,29 @@ type Controller struct { // server block as it appeared in the input. ServerBlockIndex int + // ServerBlockHostIndex is the 0-based index of this + // host as it appeared in the input at the head of the + // server block. + ServerBlockHostIndex int + // ServerBlockHosts is a list of hosts that are // associated with this server block. All these // hosts, consequently, share the same tokens. ServerBlockHosts []string + + // ServerBlockStorage is used by a directive's + // setup function to persist state between all + // the hosts on a server block. + ServerBlockStorage interface{} } // NewTestController creates a new *Controller for -// the input specified, with a filename of "Testfile" +// the input specified, with a filename of "Testfile". +// The Config is bare, consisting only of a Root of cwd. // // Used primarily for testing but needs to be exported so -// add-ons can use this as a convenience. +// add-ons can use this as a convenience. Does not initialize +// the server-block-related fields. func NewTestController(input string) *Controller { return &Controller{ Config: &server.Config{ From c0ebe31560fa0dcdbab5b9e1c3f5669ddfff852a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 18 Oct 2015 19:27:51 -0600 Subject: [PATCH 2/2] Fix ServerBlockStorage so it actually stores stuff --- config/config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/config/config.go b/config/config.go index 1e61cd9e..f1d5fcce 100644 --- a/config/config.go +++ b/config/config.go @@ -90,6 +90,7 @@ func Load(filename string, input io.Reader) (Group, error) { // TODO: For now, we only support the default path scope / config.Middleware["/"] = append(config.Middleware["/"], midware) } + storages[dir.name] = controller.ServerBlockStorage // persist for this server block } }