diff --git a/api/server/sdk/rest_gateway.go b/api/server/sdk/rest_gateway.go index d39b6175c..bfd069803 100644 --- a/api/server/sdk/rest_gateway.go +++ b/api/server/sdk/rest_gateway.go @@ -89,6 +89,19 @@ func (s *sdkRestGateway) Stop() { } } +// registerServerExtensions registers any extra server extensions +// on the SDK REST Gateway server. +func (s *sdkRestGateway) registerServerExtensions( + mux *runtime.ServeMux, conn *grpc.ClientConn) error { + for _, ext := range s.config.RestServerExtensions { + if err := ext(mux, conn); err != nil { + return err + } + } + + return nil +} + // restServerSetupHandlers sets up the handlers to the swagger ui and // to the gRPC REST Gateway. func (s *sdkRestGateway) restServerSetupHandlers() (http.Handler, error) { @@ -155,6 +168,11 @@ func (s *sdkRestGateway) restServerSetupHandlers() (http.Handler, error) { } } + // Register extra services provided + if err := s.registerServerExtensions(gmux, conn); err != nil { + return nil, err + } + // Pass all other unhandled paths to the gRPC gateway mux.Handle("/", gmux) diff --git a/api/server/sdk/server.go b/api/server/sdk/server.go index e351269e6..2f150bdea 100644 --- a/api/server/sdk/server.go +++ b/api/server/sdk/server.go @@ -25,6 +25,7 @@ import ( grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/libopenstorage/openstorage/alerts" "github.com/libopenstorage/openstorage/api" "github.com/libopenstorage/openstorage/api/spec" @@ -118,7 +119,15 @@ type ServerConfig struct { // func(gs *grpc.Server) { // api.RegisterCustomService(gs, customHandler) // }) - ServerExtensions []func(s *grpc.Server) + GrpcServerExtensions []func(grpcServer *grpc.Server) + + // RestServerExtensions allows for extensions to be added + // to the SDK Rest Gateway server. + // + // To add your own service to the SDK REST Server, simply register it + // on the http.ServeMux. You may also provide a grpc.ClientConn for + // REST Gateway services that are linked to the gRPC server. + RestServerExtensions []func(mux *runtime.ServeMux, conn *grpc.ClientConn) error } // Server is an implementation of the gRPC SDK interface @@ -464,7 +473,7 @@ func (s *sdkGrpcServer) Start() error { api.RegisterOpenStorageRoleServer(grpcServer, s.roleServer) } - s.RegisterServerExtensions(grpcServer) + s.registerServerExtensions(grpcServer) return grpcServer }) @@ -475,8 +484,8 @@ func (s *sdkGrpcServer) Start() error { return nil } -func (s *sdkGrpcServer) RegisterServerExtensions(grpcServer *grpc.Server) { - for _, ext := range s.config.ServerExtensions { +func (s *sdkGrpcServer) registerServerExtensions(grpcServer *grpc.Server) { + for _, ext := range s.config.GrpcServerExtensions { ext(grpcServer) } }