mirror of
https://github.com/minio/minio.git
synced 2025-04-09 14:10:10 -04:00
Add new RPC helpers wrapping over regular rpc packages, add middleware chaining ability
This commit is contained in:
parent
188785a886
commit
701c3e5242
@ -20,11 +20,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
router "github.com/gorilla/mux"
|
router "github.com/gorilla/mux"
|
||||||
jsonRPC "github.com/gorilla/rpc/v2"
|
|
||||||
"github.com/minio/minio/pkg/server/api"
|
"github.com/minio/minio/pkg/server/api"
|
||||||
"github.com/minio/minio/pkg/server/rpc"
|
"github.com/minio/minio/pkg/server/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// registerAPI - register all the object API handlers to their respective paths
|
||||||
func registerAPI(mux *router.Router) http.Handler {
|
func registerAPI(mux *router.Router) http.Handler {
|
||||||
api := api.MinioAPI{}
|
api := api.MinioAPI{}
|
||||||
|
|
||||||
@ -50,28 +50,68 @@ func registerAPI(mux *router.Router) http.Handler {
|
|||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerOthers(mux http.Handler, conf api.Config) http.Handler {
|
// add a handlerFunc typedef
|
||||||
mux = api.ValidContentTypeHandler(mux)
|
type handlerFunc func(http.Handler) http.Handler
|
||||||
mux = api.TimeValidityHandler(mux)
|
|
||||||
mux = api.IgnoreResourcesHandler(mux)
|
// chain struct to hold handlers
|
||||||
mux = api.ValidateAuthHeaderHandler(mux)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// registerOtherMiddleware register all available middleware
|
||||||
|
func registerOtherMiddleware(mux http.Handler, conf api.Config) http.Handler {
|
||||||
|
ch := registerChain(
|
||||||
|
api.ValidContentTypeHandler,
|
||||||
|
api.TimeValidityHandler,
|
||||||
|
api.IgnoreResourcesHandler,
|
||||||
|
api.ValidateAuthHeaderHandler,
|
||||||
|
api.LoggingHandler,
|
||||||
|
// Add new middleware here
|
||||||
|
)
|
||||||
|
|
||||||
|
mux = ch.final(mux)
|
||||||
mux = api.RateLimitHandler(mux, conf.RateLimit)
|
mux = api.RateLimitHandler(mux, conf.RateLimit)
|
||||||
mux = api.LoggingHandler(mux)
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerRPC(mux *router.Router, r *jsonRPC.Server) http.Handler {
|
// registerRPC - register rpc handlers
|
||||||
mux.Handle("/rpc", r)
|
func registerRPC(mux *router.Router, s *rpc.Server) http.Handler {
|
||||||
|
mux.Handle("/rpc", s)
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIHandler api handler
|
// APIHandler api handler
|
||||||
func APIHandler(conf api.Config) http.Handler {
|
func APIHandler(conf api.Config) http.Handler {
|
||||||
mux := router.NewRouter()
|
mux := router.NewRouter()
|
||||||
return registerOthers(registerAPI(mux), conf)
|
return registerOtherMiddleware(registerAPI(mux), conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RPCHandler rpc handler
|
// RPCHandler rpc handler
|
||||||
func RPCHandler() http.Handler {
|
func RPCHandler() http.Handler {
|
||||||
return registerRPC(router.NewRouter(), rpc.HelloServiceHandler())
|
s := rpc.NewServer()
|
||||||
|
s.RegisterJSONCodec()
|
||||||
|
s.RegisterService(new(rpc.HelloService), "")
|
||||||
|
// add more services here
|
||||||
|
return registerRPC(router.NewRouter(), s)
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,35 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/rpc/v2"
|
"github.com/gorilla/rpc/v2"
|
||||||
"github.com/gorilla/rpc/v2/json"
|
"github.com/gorilla/rpc/v2/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HelloServiceHandler -
|
// Server rpc server container
|
||||||
func HelloServiceHandler() *rpc.Server {
|
type Server struct {
|
||||||
s := rpc.NewServer()
|
RPCServer *rpc.Server
|
||||||
s.RegisterCodec(json.NewCodec(), "application/json")
|
}
|
||||||
s.RegisterService(new(HelloService), "")
|
|
||||||
|
// RegisterJSONCodec - register standard json codec
|
||||||
|
func (s Server) RegisterJSONCodec() {
|
||||||
|
s.RPCServer.RegisterCodec(json.NewCodec(), "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterService - register new services
|
||||||
|
func (s Server) RegisterService(recv interface{}, name string) {
|
||||||
|
s.RPCServer.RegisterService(recv, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewServer - provide a new instance of RPC server
|
||||||
|
func NewServer() *Server {
|
||||||
|
s := &Server{}
|
||||||
|
s.RPCServer = rpc.NewServer()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeHTTP wrapper method for http.Handler interface
|
||||||
|
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.RPCServer.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
@ -28,7 +28,6 @@ import (
|
|||||||
func startAPI(errCh chan error, conf api.Config) {
|
func startAPI(errCh chan error, conf api.Config) {
|
||||||
defer close(errCh)
|
defer close(errCh)
|
||||||
|
|
||||||
var err error
|
|
||||||
// Minio server config
|
// Minio server config
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: conf.Address,
|
Addr: conf.Address,
|
||||||
@ -66,37 +65,26 @@ func startAPI(errCh chan error, conf api.Config) {
|
|||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
fmt.Printf("Starting minio server on: http://%s:%s\n", host, port)
|
fmt.Printf("Starting minio server on: http://%s:%s\n", host, port)
|
||||||
}
|
}
|
||||||
err = httpServer.ListenAndServe()
|
errCh <- httpServer.ListenAndServe()
|
||||||
case conf.TLS == true:
|
case conf.TLS == true:
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
fmt.Printf("Starting minio server on: https://%s:%s\n", host, port)
|
fmt.Printf("Starting minio server on: https://%s:%s\n", host, port)
|
||||||
}
|
}
|
||||||
err = httpServer.ListenAndServeTLS(conf.CertFile, conf.KeyFile)
|
errCh <- httpServer.ListenAndServeTLS(conf.CertFile, conf.KeyFile)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
errCh <- err
|
|
||||||
}
|
|
||||||
errCh <- nil
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startRPC(errCh chan error) {
|
func startRPC(errCh chan error) {
|
||||||
defer close(errCh)
|
defer close(errCh)
|
||||||
|
|
||||||
rpcHandler := RPCHandler()
|
rpcHandler := RPCHandler()
|
||||||
var err error
|
|
||||||
// Minio server config
|
// Minio server config
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: "127.0.0.1:9001",
|
Addr: "127.0.0.1:9001", // TODO make this configurable
|
||||||
Handler: rpcHandler,
|
Handler: rpcHandler,
|
||||||
MaxHeaderBytes: 1 << 20,
|
MaxHeaderBytes: 1 << 20,
|
||||||
}
|
}
|
||||||
err = httpServer.ListenAndServe()
|
errCh <- httpServer.ListenAndServe()
|
||||||
if err != nil {
|
|
||||||
errCh <- err
|
|
||||||
}
|
|
||||||
errCh <- nil
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartServices starts basic services for a server
|
// StartServices starts basic services for a server
|
||||||
@ -109,13 +97,8 @@ func StartServices(conf api.Config) error {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-apiErrCh:
|
case err := <-apiErrCh:
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
case err := <-rpcErrCh:
|
case err := <-rpcErrCh:
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user