Merge pull request #826 from krishnasrinivas/middleware-cleanup

Remove unneeded functions in middleware init
master
Harshavardhana 9 years ago
commit 8d5f6e0b96
  1. 2
      pkg/server/api/generic-handlers.go
  2. 49
      pkg/server/router.go

@ -25,6 +25,8 @@ import (
"github.com/rs/cors" "github.com/rs/cors"
) )
type MiddlewareHandler func(http.Handler) http.Handler
type contentTypeHandler struct { type contentTypeHandler struct {
handler http.Handler handler http.Handler
} }

@ -25,7 +25,7 @@ import (
) )
// registerAPI - register all the object API handlers to their respective paths // registerAPI - register all the object API handlers to their respective paths
func registerAPI(mux *router.Router, a api.Minio) http.Handler { func registerAPI(mux *router.Router, a api.Minio) {
mux.HandleFunc("/", a.ListBucketsHandler).Methods("GET") mux.HandleFunc("/", a.ListBucketsHandler).Methods("GET")
mux.HandleFunc("/{bucket}", a.ListObjectsHandler).Methods("GET") mux.HandleFunc("/{bucket}", a.ListObjectsHandler).Methods("GET")
mux.HandleFunc("/{bucket}", a.PutBucketHandler).Methods("PUT") mux.HandleFunc("/{bucket}", a.PutBucketHandler).Methods("PUT")
@ -44,61 +44,32 @@ func registerAPI(mux *router.Router, a api.Minio) http.Handler {
// unsupported API // unsupported API
mux.HandleFunc("/{bucket}/{object:.*}", a.DeleteObjectHandler).Methods("DELETE") mux.HandleFunc("/{bucket}/{object:.*}", a.DeleteObjectHandler).Methods("DELETE")
return mux
}
// add a handlerFunc typedef
type handlerFunc func(http.Handler) http.Handler
// chain struct to hold handlers
type chain struct {
handlers []handlerFunc
} }
// loop through handlers and return a final one func registerCustomMiddleware(mux *router.Router, mwHandlers ...api.MiddlewareHandler) http.Handler {
func (c chain) final(mux http.Handler) http.Handler {
var f http.Handler var f http.Handler
if mux != nil {
f = mux f = mux
} else { for _, mw := range mwHandlers {
f = http.DefaultServeMux f = mw(f)
}
for _, handler := range c.handlers {
f = handler(f)
} }
return f return f
} }
// registerChain - register an array of handlers in a chain of style -> handler(handler(handler(handler...))) // getAPIHandler api handler
func registerChain(handlers ...handlerFunc) chain { func getAPIHandler(conf api.Config) (http.Handler, api.Minio) {
ch := chain{} var mwHandlers = []api.MiddlewareHandler{
ch.handlers = append(ch.handlers, handlers...)
return ch
}
// registerCustomMiddleware register all available custom middleware
func registerCustomMiddleware(mux http.Handler, conf api.Config) http.Handler {
ch := registerChain(
api.ValidContentTypeHandler, api.ValidContentTypeHandler,
api.TimeValidityHandler, api.TimeValidityHandler,
api.IgnoreResourcesHandler, api.IgnoreResourcesHandler,
api.ValidateAuthHeaderHandler, api.ValidateAuthHeaderHandler,
// api.LoggingHandler, // Disabled logging until we bring in external logging support // api.LoggingHandler, // Disabled logging until we bring in external logging support
api.CorsHandler, api.CorsHandler,
// Add new your new middleware here }
)
mux = ch.final(mux)
return mux
}
// getAPIHandler api handler
func getAPIHandler(conf api.Config) (http.Handler, api.Minio) {
mux := router.NewRouter() mux := router.NewRouter()
minioAPI := api.New() minioAPI := api.New()
apiHandler := registerAPI(mux, minioAPI) registerAPI(mux, minioAPI)
apiHandler = registerCustomMiddleware(apiHandler, conf) apiHandler := registerCustomMiddleware(mux, mwHandlers...)
return apiHandler, minioAPI return apiHandler, minioAPI
} }

Loading…
Cancel
Save