handlers: Fix the naming of all handlers.

This commit is contained in:
Harshavardhana 2016-02-04 14:57:20 -08:00
parent 4d97c042da
commit 012fbe756b
5 changed files with 36 additions and 36 deletions

View File

@ -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)

View File

@ -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}
}

View File

@ -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") == "" {

View File

@ -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...)
}

View File

@ -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}
}