Skip to content

Commit

Permalink
Add support for REST server extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Griffiths <[email protected]>
  • Loading branch information
ggriffiths committed May 2, 2019
1 parent 71b7f37 commit 98fe9fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
18 changes: 18 additions & 0 deletions api/server/sdk/rest_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 13 additions & 4 deletions api/server/sdk/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -464,7 +473,7 @@ func (s *sdkGrpcServer) Start() error {
api.RegisterOpenStorageRoleServer(grpcServer, s.roleServer)
}

s.RegisterServerExtensions(grpcServer)
s.registerServerExtensions(grpcServer)

return grpcServer
})
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 98fe9fe

Please sign in to comment.