From 93e40accd16fc21e15ec9f44f21b0f05af2011ac Mon Sep 17 00:00:00 2001 From: Grant Griffiths Date: Thu, 2 May 2019 15:55:26 -0700 Subject: [PATCH] Add support for REST server extensions Signed-off-by: Grant Griffiths --- api/server/sdk/rest_gateway.go | 3 +++ api/server/sdk/server.go | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/api/server/sdk/rest_gateway.go b/api/server/sdk/rest_gateway.go index d39b6175c..b6840b332 100644 --- a/api/server/sdk/rest_gateway.go +++ b/api/server/sdk/rest_gateway.go @@ -147,6 +147,9 @@ func (s *sdkRestGateway) restServerSetupHandlers() (http.Handler, error) { api.RegisterOpenStoragePolicyHandler, } + // REST Gateway extensions + handlers = append(handlers, s.config.RestServerExtensions...) + // Register the REST Gateway handlers for _, handler := range handlers { err := handler(context.Background(), gmux, conn) diff --git a/api/server/sdk/server.go b/api/server/sdk/server.go index e351269e6..7e357f979 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 add your handlers + // to the RestSererExtensions slice. These handlers will be registered on the + // REST Gateway http server. + RestServerExtensions []func(context.Context, *runtime.ServeMux, *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) } }