From 012fbe756bdc945742b8af0d28289029a6cd5d8d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 4 Feb 2016 14:57:20 -0800 Subject: [PATCH] handlers: Fix the naming of all handlers. --- accesslog-handler.go | 4 ++-- generic-handlers.go | 26 +++++++++++++------------- jwt-auth-handler.go | 10 +++++----- routers.go | 28 ++++++++++++++-------------- signature-handler.go | 4 ++-- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/accesslog-handler.go b/accesslog-handler.go index 6bf23431d..cb28b39f6 100644 --- a/accesslog-handler.go +++ b/accesslog-handler.go @@ -111,8 +111,8 @@ func getLogMessage(w http.ResponseWriter, req *http.Request) ([]byte, *probe.Err return js, nil } -// AccessLogHandler logs requests -func AccessLogHandler(h http.Handler) http.Handler { +// setAccessLogHandler logs requests +func setAccessLogHandler(h http.Handler) http.Handler { file, e := os.OpenFile("access.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) fatalIf(probe.NewError(e), "Unable to open access log.", nil) diff --git a/generic-handlers.go b/generic-handlers.go index 13c01437c..8709f1a74 100644 --- a/generic-handlers.go +++ b/generic-handlers.go @@ -26,14 +26,14 @@ import ( "github.com/rs/cors" ) -// MiddlewareHandler - useful to chain different middleware http.Handler -type MiddlewareHandler func(http.Handler) http.Handler +// HandlerFunc - useful to chain different middleware http.Handler +type HandlerFunc func(http.Handler) http.Handler -func registerCustomMiddleware(mux *router.Router, mwHandlers ...MiddlewareHandler) http.Handler { +func registerHandlers(mux *router.Router, handlerFns ...HandlerFunc) http.Handler { var f http.Handler f = mux - for _, mw := range mwHandlers { - f = mw(f) + for _, hFn := range handlerFns { + f = hFn(f) } return f } @@ -110,8 +110,8 @@ func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.handler.ServeHTTP(w, r) } -// TimeValidityHandler to validate parsable time over http header -func TimeValidityHandler(h http.Handler) http.Handler { +// setTimeValidityHandler to validate parsable time over http header +func setTimeValidityHandler(h http.Handler) http.Handler { return timeHandler{h} } @@ -139,8 +139,8 @@ func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.handler.ServeHTTP(w, r) } -// CorsHandler handler for CORS (Cross Origin Resource Sharing) -func CorsHandler(h http.Handler) http.Handler { +// setCorsHandler handler for CORS (Cross Origin Resource Sharing) +func setCorsHandler(h http.Handler) http.Handler { c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "HEAD", "POST", "PUT"}, @@ -149,9 +149,9 @@ func CorsHandler(h http.Handler) http.Handler { return c.Handler(h) } -// IgnoreSignatureV2RequestHandler - +// setIgnoreSignatureV2RequestHandler - // Verify if authorization header has signature version '2', reject it cleanly. -func IgnoreSignatureV2RequestHandler(h http.Handler) http.Handler { +func setIgnoreSignatureV2RequestHandler(h http.Handler) http.Handler { return ignoreSignatureV2RequestHandler{h} } @@ -166,11 +166,11 @@ func (h ignoreSignatureV2RequestHandler) ServeHTTP(w http.ResponseWriter, r *htt h.handler.ServeHTTP(w, r) } -// IgnoreResourcesHandler - +// setIgnoreResourcesHandler - // Ignore resources handler is wrapper handler used for API request resource validation // Since we do not support all the S3 queries, it is necessary for us to throw back a // valid error message indicating such a feature is not implemented. -func IgnoreResourcesHandler(h http.Handler) http.Handler { +func setIgnoreResourcesHandler(h http.Handler) http.Handler { return resourceHandler{h} } diff --git a/jwt-auth-handler.go b/jwt-auth-handler.go index a944b187c..bc64676b8 100644 --- a/jwt-auth-handler.go +++ b/jwt-auth-handler.go @@ -23,18 +23,18 @@ import ( jwtgo "github.com/dgrijalva/jwt-go" ) -type authHandler struct { +type jwtAuthHandler struct { handler http.Handler } -// AuthHandler - +// setJWTAuthHandler - // Verify if authorization header is of form JWT, reject it otherwise. -func AuthHandler(h http.Handler) http.Handler { - return authHandler{h} +func setJWTAuthHandler(h http.Handler) http.Handler { + return jwtAuthHandler{h} } // Ignore request if authorization header is not valid. -func (h authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h jwtAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Let the top level caller handle if the requests should be // allowed, if there are no Authorization headers. if r.Header.Get("Authorization") == "" { diff --git a/routers.go b/routers.go index c368fcec2..7d945c8be 100644 --- a/routers.go +++ b/routers.go @@ -54,14 +54,14 @@ type WebAPI struct { } func getWebAPIHandler(web *WebAPI) http.Handler { - var mwHandlers = []MiddlewareHandler{ + var handlerFns = []HandlerFunc{ setCacheControlHandler, // Adds Cache-Control header - TimeValidityHandler, // Validate time. - AuthHandler, // Authentication handler for verifying tokens. - CorsHandler, // CORS added only for testing purposes. + setTimeValidityHandler, // Validate time. + setJWTAuthHandler, // Authentication handler for verifying JWT's. + setCorsHandler, // CORS added only for testing purposes. } if web.AccessLog { - mwHandlers = append(mwHandlers, AccessLogHandler) + handlerFns = append(handlerFns, setAccessLogHandler) } s := jsonrpc.NewServer() @@ -77,7 +77,7 @@ func getWebAPIHandler(web *WebAPI) http.Handler { // Enable this when we add assets. // root.PathPrefix("/login").Handler(http.StripPrefix("/login", http.FileServer(assetFS()))) // root.Handle("/{file:.*}", http.FileServer(assetFS())) - return registerCustomMiddleware(mux, mwHandlers...) + return registerHandlers(mux, handlerFns...) } // registerCloudStorageAPI - register all the handlers to their respective paths @@ -154,17 +154,17 @@ func getNewCloudStorageAPI(conf cloudServerConfig) CloudStorageAPI { } func getCloudStorageAPIHandler(api CloudStorageAPI) http.Handler { - var mwHandlers = []MiddlewareHandler{ - TimeValidityHandler, - IgnoreResourcesHandler, - IgnoreSignatureV2RequestHandler, - SignatureHandler, + var handlerFns = []HandlerFunc{ + setTimeValidityHandler, + setIgnoreResourcesHandler, + setIgnoreSignatureV2RequestHandler, + setSignatureHandler, } if api.AccessLog { - mwHandlers = append(mwHandlers, AccessLogHandler) + handlerFns = append(handlerFns, setAccessLogHandler) } - mwHandlers = append(mwHandlers, CorsHandler) + handlerFns = append(handlerFns, setCorsHandler) mux := router.NewRouter() registerCloudStorageAPI(mux, api) - return registerCustomMiddleware(mux, mwHandlers...) + return registerHandlers(mux, handlerFns...) } diff --git a/signature-handler.go b/signature-handler.go index 103a9fbc0..f5dd1edc2 100644 --- a/signature-handler.go +++ b/signature-handler.go @@ -30,8 +30,8 @@ type signatureHandler struct { handler http.Handler } -// SignatureHandler to validate authorization header for the incoming request. -func SignatureHandler(h http.Handler) http.Handler { +// setSignatureHandler to validate authorization header for the incoming request. +func setSignatureHandler(h http.Handler) http.Handler { return signatureHandler{h} }