From 4e9c432c14a77dd74915876926f4dd86dec66f22 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 29 Mar 2015 19:56:19 -0600 Subject: [PATCH] Controller/Dispenser refactoring, typo fixes --- middleware/errors/errors.go | 1 + middleware/middleware.go | 50 ++++++++++++++++++--------------- middleware/redirect/redirect.go | 2 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/middleware/errors/errors.go b/middleware/errors/errors.go index 506f36082..149f66f1e 100644 --- a/middleware/errors/errors.go +++ b/middleware/errors/errors.go @@ -97,6 +97,7 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, code int) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(code) _, err = io.Copy(w, errorPage) + if err != nil { // Epic fail... sigh. h.Log.Printf("HTTP %d could not respond with %s: %v", code, pagePath, err) diff --git a/middleware/middleware.go b/middleware/middleware.go index ca570441b..6fa4fe623 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -37,10 +37,35 @@ type ( ServeHTTP(http.ResponseWriter, *http.Request) (int, error) } - // A Control provides structured access to tokens from a configuration file - // and also to properties of the server being configured. Middleware generators - // use a Controller to construct their middleware instance. + // A Controller provides access to properties of the server. Middleware + // generators use a Controller to construct their instances. Controller interface { + Dispenser + + // Startup registers a function to execute when the server starts. + Startup(func() error) + + // Shutdown registers a function to execute when the server exits. + Shutdown(func() error) + + // Root returns the file path from which the server is serving. + Root() string + + // Host returns the hostname the server is bound to. + Host() string + + // Port returns the port that the server is listening on. + Port() string + + // Context returns the path scope that the Controller is in. + // Note: This is not currently used, but may be in the future. + Context() Path + } + + // A Dispenser provides structured access to tokens from a configuration + // file. It dispenses tokens to middleware for parsing so that middleware + // can configure themselves. + Dispenser interface { // Next loads the next token. Returns true if a token // was loaded; false otherwise. If false, all tokens // have already been consumed. @@ -91,24 +116,5 @@ type ( // Err generates a custom parse error with a message of msg. Err(string) error - - // Startup registers a function to execute when the server starts. - Startup(func() error) - - // Shutdown registers a function to execute when the server exits. - Shutdown(func() error) - - // Root returns the file path from which the server is serving. - Root() string - - // Host returns the hostname the server is bound to. - Host() string - - // Port returns the port that the server is listening on. - Port() string - - // Context returns the path scope that the Controller is in. - // Note: This is not currently used, but may be in the future. - Context() Path } ) diff --git a/middleware/redirect/redirect.go b/middleware/redirect/redirect.go index e9ec79498..59f72cabd 100644 --- a/middleware/redirect/redirect.go +++ b/middleware/redirect/redirect.go @@ -46,7 +46,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) { for _, rule := range redirects { if r.URL.Path == rule.From { http.Redirect(w, r, rule.To, rule.Code) - break + return 0, nil } } return next(w, r)