From 1ef7f3c4b1c4f11d04888efb84e98a880b7d5c6a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 17 Feb 2016 18:11:03 -0700 Subject: [PATCH] Remove path scoping for middleware slice It was implemented for almost a year but we'll probably never use it, especially since we'll match more than the path in the future. --- caddy/config.go | 8 ++------ caddy/https/https.go | 10 ++++------ caddy/https/https_test.go | 6 +++--- server/config.go | 4 ++-- server/virtualhost.go | 8 +------- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/caddy/config.go b/caddy/config.go index 15420e31..c8ea6b4d 100644 --- a/caddy/config.go +++ b/caddy/config.go @@ -11,7 +11,6 @@ import ( "github.com/mholt/caddy/caddy/https" "github.com/mholt/caddy/caddy/parse" "github.com/mholt/caddy/caddy/setup" - "github.com/mholt/caddy/middleware" "github.com/mholt/caddy/server" ) @@ -55,7 +54,6 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con Port: addr.Port, Scheme: addr.Scheme, Root: Root, - Middleware: make(map[string][]middleware.Middleware), ConfigFile: filename, AppName: AppName, AppVersion: AppVersion, @@ -89,8 +87,7 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con return nil, nil, lastDirectiveIndex, err } if midware != nil { - // TODO: For now, we only support the default path scope / - config.Middleware["/"] = append(config.Middleware["/"], midware) + config.Middleware = append(config.Middleware, midware) } storages[dir.name] = controller.ServerBlockStorage // persist for this server block } @@ -171,8 +168,7 @@ func loadConfigs(filename string, input io.Reader) ([]server.Config, error) { return nil, err } if midware != nil { - // TODO: For now, we only support the default path scope / - configs[configIndex].Middleware["/"] = append(configs[configIndex].Middleware["/"], midware) + configs[configIndex].Middleware = append(configs[configIndex].Middleware, midware) } storages[dir.name] = controller.ServerBlockStorage // persist for this server block } diff --git a/caddy/https/https.go b/caddy/https/https.go index 4526a31c..50ed53d6 100644 --- a/caddy/https/https.go +++ b/caddy/https/https.go @@ -332,12 +332,10 @@ func redirPlaintextHost(cfg server.Config) server.Config { } return server.Config{ - Host: cfg.Host, - BindHost: cfg.BindHost, - Port: "80", - Middleware: map[string][]middleware.Middleware{ - "/": {redirMidware}, - }, + Host: cfg.Host, + BindHost: cfg.BindHost, + Port: "80", + Middleware: []middleware.Middleware{redirMidware}, } } diff --git a/caddy/https/https_test.go b/caddy/https/https_test.go index 199c6266..e06af138 100644 --- a/caddy/https/https_test.go +++ b/caddy/https/https_test.go @@ -87,11 +87,11 @@ func TestRedirPlaintextHost(t *testing.T) { } // Make sure redirect handler is set up properly - if cfg.Middleware == nil || len(cfg.Middleware["/"]) != 1 { + if cfg.Middleware == nil || len(cfg.Middleware) != 1 { t.Fatalf("Redir config middleware not set up properly; got: %#v", cfg.Middleware) } - handler, ok := cfg.Middleware["/"][0](nil).(redirect.Redirect) + handler, ok := cfg.Middleware[0](nil).(redirect.Redirect) if !ok { t.Fatalf("Expected a redirect.Redirect middleware, but got: %#v", handler) } @@ -116,7 +116,7 @@ func TestRedirPlaintextHost(t *testing.T) { // browsers can infer a default port from scheme, so make sure the port // doesn't get added in explicitly for default ports like 443 for https. cfg = redirPlaintextHost(server.Config{Host: "example.com", Port: "443"}) - handler, ok = cfg.Middleware["/"][0](nil).(redirect.Redirect) + handler, ok = cfg.Middleware[0](nil).(redirect.Redirect) if actual, expected := handler.Rules[0].To, "https://{host}{uri}"; actual != expected { t.Errorf("(Default Port) Expected redirect rule to be to URL '%s' but is actually to '%s'", expected, actual) } diff --git a/server/config.go b/server/config.go index 332f4575..1f4acdb6 100644 --- a/server/config.go +++ b/server/config.go @@ -26,8 +26,8 @@ type Config struct { // HTTPS configuration TLS TLSConfig - // Middleware stack; map of path scope to middleware -- TODO: Support path scope? - Middleware map[string][]middleware.Middleware + // Middleware stack + Middleware []middleware.Middleware // Startup is a list of functions (or methods) to execute at // server startup and restart; these are executed before any diff --git a/server/virtualhost.go b/server/virtualhost.go index b0d15797..0f44cc68 100644 --- a/server/virtualhost.go +++ b/server/virtualhost.go @@ -21,13 +21,7 @@ type virtualHost struct { // ListenAndServe begins. func (vh *virtualHost) buildStack() error { vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile}) - - // TODO: We only compile middleware for the "/" scope. - // Partial support for multiple location contexts already - // exists at the parser and config levels, but until full - // support is implemented, this is all we do right here. - vh.compile(vh.config.Middleware["/"]) - + vh.compile(vh.config.Middleware) return nil }