2015-06-30 23:15:48 -04:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
router "github.com/gorilla/mux"
|
|
|
|
"github.com/minio/minio/pkg/server/api"
|
|
|
|
"github.com/minio/minio/pkg/server/rpc"
|
|
|
|
)
|
|
|
|
|
2015-07-01 03:37:43 -04:00
|
|
|
// registerAPI - register all the object API handlers to their respective paths
|
2015-07-02 18:40:16 -04:00
|
|
|
func registerAPI(mux *router.Router, a api.Minio) http.Handler {
|
|
|
|
mux.HandleFunc("/", a.ListBucketsHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}", a.ListObjectsHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}", a.PutBucketHandler).Methods("PUT")
|
|
|
|
mux.HandleFunc("/{bucket}", a.HeadBucketHandler).Methods("HEAD")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.HeadObjectHandler).Methods("HEAD")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}").Methods("PUT")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}").Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("POST")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.NewMultipartUploadHandler).Methods("POST")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("DELETE")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.GetObjectHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.PutObjectHandler).Methods("PUT")
|
2015-06-30 23:15:48 -04:00
|
|
|
|
|
|
|
// not implemented yet
|
2015-07-02 18:40:16 -04:00
|
|
|
mux.HandleFunc("/{bucket}", a.DeleteBucketHandler).Methods("DELETE")
|
2015-06-30 23:15:48 -04:00
|
|
|
|
|
|
|
// unsupported API
|
2015-07-02 18:40:16 -04:00
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", a.DeleteObjectHandler).Methods("DELETE")
|
2015-06-30 23:15:48 -04:00
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-07-01 03:37:43 -04:00
|
|
|
// 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 (c chain) final(mux http.Handler) http.Handler {
|
|
|
|
var f http.Handler
|
|
|
|
if mux != nil {
|
|
|
|
f = mux
|
|
|
|
} else {
|
|
|
|
f = http.DefaultServeMux
|
|
|
|
}
|
|
|
|
for _, handler := range c.handlers {
|
|
|
|
f = handler(f)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerChain - register an array of handlers in a chain of style -> handler(handler(handler(handler...)))
|
|
|
|
func registerChain(handlers ...handlerFunc) chain {
|
|
|
|
ch := chain{}
|
|
|
|
ch.handlers = append(ch.handlers, handlers...)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
// registerCustomMiddleware register all available custom middleware
|
|
|
|
func registerCustomMiddleware(mux http.Handler, conf api.Config) http.Handler {
|
2015-07-01 03:37:43 -04:00
|
|
|
ch := registerChain(
|
|
|
|
api.ValidContentTypeHandler,
|
|
|
|
api.TimeValidityHandler,
|
|
|
|
api.IgnoreResourcesHandler,
|
|
|
|
api.ValidateAuthHeaderHandler,
|
|
|
|
api.LoggingHandler,
|
2015-07-02 23:31:22 -04:00
|
|
|
// Add new your new middleware here
|
2015-07-01 03:37:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
mux = ch.final(mux)
|
2015-06-30 23:15:48 -04:00
|
|
|
mux = api.RateLimitHandler(mux, conf.RateLimit)
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-07-01 03:37:43 -04:00
|
|
|
// registerRPC - register rpc handlers
|
|
|
|
func registerRPC(mux *router.Router, s *rpc.Server) http.Handler {
|
|
|
|
mux.Handle("/rpc", s)
|
2015-06-30 23:15:48 -04:00
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
// getAPIHandler api handler
|
2015-07-02 18:40:16 -04:00
|
|
|
func getAPIHandler(conf api.Config) (http.Handler, api.Minio) {
|
2015-06-30 23:15:48 -04:00
|
|
|
mux := router.NewRouter()
|
2015-07-05 19:55:55 -04:00
|
|
|
minioAPI := api.New()
|
2015-07-02 18:40:16 -04:00
|
|
|
apiHandler := registerAPI(mux, minioAPI)
|
2015-07-02 23:31:22 -04:00
|
|
|
apiHandler = registerCustomMiddleware(apiHandler, conf)
|
2015-07-02 18:40:16 -04:00
|
|
|
return apiHandler, minioAPI
|
2015-06-30 23:15:48 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
// getRPCHandler rpc handler
|
|
|
|
func getRPCHandler() http.Handler {
|
2015-07-01 03:37:43 -04:00
|
|
|
s := rpc.NewServer()
|
|
|
|
s.RegisterJSONCodec()
|
2015-07-05 13:19:23 -04:00
|
|
|
s.RegisterService(new(rpc.VersionService), "Version")
|
|
|
|
s.RegisterService(new(rpc.SysInfoService), "SysInfo")
|
2015-07-07 19:59:20 -04:00
|
|
|
s.RegisterService(new(rpc.MemStatsService), "MemStats")
|
2015-07-05 13:19:23 -04:00
|
|
|
s.RegisterService(new(rpc.DiskInfoService), "DiskInfo")
|
2015-07-06 13:35:23 -04:00
|
|
|
s.RegisterService(new(rpc.DonutService), "Donut")
|
2015-07-07 19:59:20 -04:00
|
|
|
// Add new RPC services here
|
2015-07-01 03:37:43 -04:00
|
|
|
return registerRPC(router.NewRouter(), s)
|
2015-06-30 23:15:48 -04:00
|
|
|
}
|