Remove unneeded functions in middleware init

This commit is contained in:
Krishna Srinivas 2015-09-15 16:59:43 -07:00
parent ba6ddd5924
commit b1b387b157
2 changed files with 13 additions and 40 deletions

View File

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

View File

@ -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 func registerCustomMiddleware(mux *router.Router, mwHandlers ...api.MiddlewareHandler) http.Handler {
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 (c chain) final(mux http.Handler) http.Handler {
var f http.Handler var f http.Handler
if mux != nil { f = mux
f = mux for _, mw := range mwHandlers {
} else { f = mw(f)
f = http.DefaultServeMux
}
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
} }