handlers: Fix the naming of all handlers.

master
Harshavardhana 9 years ago
parent 4d97c042da
commit 012fbe756b
  1. 4
      accesslog-handler.go
  2. 26
      generic-handlers.go
  3. 10
      jwt-auth-handler.go
  4. 28
      routers.go
  5. 4
      signature-handler.go

@ -111,8 +111,8 @@ func getLogMessage(w http.ResponseWriter, req *http.Request) ([]byte, *probe.Err
return js, nil return js, nil
} }
// AccessLogHandler logs requests // setAccessLogHandler logs requests
func AccessLogHandler(h http.Handler) http.Handler { func setAccessLogHandler(h http.Handler) http.Handler {
file, e := os.OpenFile("access.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) 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) fatalIf(probe.NewError(e), "Unable to open access log.", nil)

@ -26,14 +26,14 @@ import (
"github.com/rs/cors" "github.com/rs/cors"
) )
// MiddlewareHandler - useful to chain different middleware http.Handler // HandlerFunc - useful to chain different middleware http.Handler
type MiddlewareHandler func(http.Handler) 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 var f http.Handler
f = mux f = mux
for _, mw := range mwHandlers { for _, hFn := range handlerFns {
f = mw(f) f = hFn(f)
} }
return f return f
} }
@ -110,8 +110,8 @@ func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handler.ServeHTTP(w, r) h.handler.ServeHTTP(w, r)
} }
// TimeValidityHandler to validate parsable time over http header // setTimeValidityHandler to validate parsable time over http header
func TimeValidityHandler(h http.Handler) http.Handler { func setTimeValidityHandler(h http.Handler) http.Handler {
return timeHandler{h} return timeHandler{h}
} }
@ -139,8 +139,8 @@ func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handler.ServeHTTP(w, r) h.handler.ServeHTTP(w, r)
} }
// CorsHandler handler for CORS (Cross Origin Resource Sharing) // setCorsHandler handler for CORS (Cross Origin Resource Sharing)
func CorsHandler(h http.Handler) http.Handler { func setCorsHandler(h http.Handler) http.Handler {
c := cors.New(cors.Options{ c := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT"}, AllowedMethods: []string{"GET", "HEAD", "POST", "PUT"},
@ -149,9 +149,9 @@ func CorsHandler(h http.Handler) http.Handler {
return c.Handler(h) return c.Handler(h)
} }
// IgnoreSignatureV2RequestHandler - // setIgnoreSignatureV2RequestHandler -
// Verify if authorization header has signature version '2', reject it cleanly. // 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} return ignoreSignatureV2RequestHandler{h}
} }
@ -166,11 +166,11 @@ func (h ignoreSignatureV2RequestHandler) ServeHTTP(w http.ResponseWriter, r *htt
h.handler.ServeHTTP(w, r) h.handler.ServeHTTP(w, r)
} }
// IgnoreResourcesHandler - // setIgnoreResourcesHandler -
// Ignore resources handler is wrapper handler used for API request resource validation // 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 // 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. // 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} return resourceHandler{h}
} }

@ -23,18 +23,18 @@ import (
jwtgo "github.com/dgrijalva/jwt-go" jwtgo "github.com/dgrijalva/jwt-go"
) )
type authHandler struct { type jwtAuthHandler struct {
handler http.Handler handler http.Handler
} }
// AuthHandler - // setJWTAuthHandler -
// Verify if authorization header is of form JWT, reject it otherwise. // Verify if authorization header is of form JWT, reject it otherwise.
func AuthHandler(h http.Handler) http.Handler { func setJWTAuthHandler(h http.Handler) http.Handler {
return authHandler{h} return jwtAuthHandler{h}
} }
// Ignore request if authorization header is not valid. // 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 // Let the top level caller handle if the requests should be
// allowed, if there are no Authorization headers. // allowed, if there are no Authorization headers.
if r.Header.Get("Authorization") == "" { if r.Header.Get("Authorization") == "" {

@ -54,14 +54,14 @@ type WebAPI struct {
} }
func getWebAPIHandler(web *WebAPI) http.Handler { func getWebAPIHandler(web *WebAPI) http.Handler {
var mwHandlers = []MiddlewareHandler{ var handlerFns = []HandlerFunc{
setCacheControlHandler, // Adds Cache-Control header setCacheControlHandler, // Adds Cache-Control header
TimeValidityHandler, // Validate time. setTimeValidityHandler, // Validate time.
AuthHandler, // Authentication handler for verifying tokens. setJWTAuthHandler, // Authentication handler for verifying JWT's.
CorsHandler, // CORS added only for testing purposes. setCorsHandler, // CORS added only for testing purposes.
} }
if web.AccessLog { if web.AccessLog {
mwHandlers = append(mwHandlers, AccessLogHandler) handlerFns = append(handlerFns, setAccessLogHandler)
} }
s := jsonrpc.NewServer() s := jsonrpc.NewServer()
@ -77,7 +77,7 @@ func getWebAPIHandler(web *WebAPI) http.Handler {
// Enable this when we add assets. // Enable this when we add assets.
// root.PathPrefix("/login").Handler(http.StripPrefix("/login", http.FileServer(assetFS()))) // root.PathPrefix("/login").Handler(http.StripPrefix("/login", http.FileServer(assetFS())))
// root.Handle("/{file:.*}", 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 // registerCloudStorageAPI - register all the handlers to their respective paths
@ -154,17 +154,17 @@ func getNewCloudStorageAPI(conf cloudServerConfig) CloudStorageAPI {
} }
func getCloudStorageAPIHandler(api CloudStorageAPI) http.Handler { func getCloudStorageAPIHandler(api CloudStorageAPI) http.Handler {
var mwHandlers = []MiddlewareHandler{ var handlerFns = []HandlerFunc{
TimeValidityHandler, setTimeValidityHandler,
IgnoreResourcesHandler, setIgnoreResourcesHandler,
IgnoreSignatureV2RequestHandler, setIgnoreSignatureV2RequestHandler,
SignatureHandler, setSignatureHandler,
} }
if api.AccessLog { if api.AccessLog {
mwHandlers = append(mwHandlers, AccessLogHandler) handlerFns = append(handlerFns, setAccessLogHandler)
} }
mwHandlers = append(mwHandlers, CorsHandler) handlerFns = append(handlerFns, setCorsHandler)
mux := router.NewRouter() mux := router.NewRouter()
registerCloudStorageAPI(mux, api) registerCloudStorageAPI(mux, api)
return registerCustomMiddleware(mux, mwHandlers...) return registerHandlers(mux, handlerFns...)
} }

@ -30,8 +30,8 @@ type signatureHandler struct {
handler http.Handler handler http.Handler
} }
// SignatureHandler to validate authorization header for the incoming request. // setSignatureHandler to validate authorization header for the incoming request.
func SignatureHandler(h http.Handler) http.Handler { func setSignatureHandler(h http.Handler) http.Handler {
return signatureHandler{h} return signatureHandler{h}
} }

Loading…
Cancel
Save