Skip to content

Commit

Permalink
Merge a39b723 into 8b0f780
Browse files Browse the repository at this point in the history
  • Loading branch information
jprobinson committed Jul 8, 2017
2 parents 8b0f780 + a39b723 commit 31968b2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
35 changes: 18 additions & 17 deletions server/kit/kitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"net/http"
"os"

ocontext "golang.org/x/net/context"
"google.golang.org/grpc"

"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pkg/errors"
ocontext "golang.org/x/net/context"
"google.golang.org/grpc"
)

// Server encapsulates all logic for registering and running a gizmo kit server.
Expand Down Expand Up @@ -137,22 +136,24 @@ func (s *Server) register(svc Service) {
return
}

gopts := []grpc.ServerOption{
grpc.UnaryInterceptor(
grpc_middleware.ChainUnaryServer(
grpc.UnaryServerInterceptor(
// inject logger into gRPC server and hook in go-kit middleware
func(ctx ocontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
ctx = context.WithValue(ctx, logKey, s.logger)
return svc.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) {
return handler(ctx, req)
})(ctx, req)
}),
),
inters := []grpc.UnaryServerInterceptor{
grpc.UnaryServerInterceptor(
// inject logger into gRPC server and hook in go-kit middleware
func(ctx ocontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
ctx = context.WithValue(ctx, logKey, s.logger)
return svc.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) {
return handler(ctx, req)
})(ctx, req)
},
),
}
gopts = append(gopts, svc.RPCOptions()...)
s.gsvr = grpc.NewServer(gopts...)
if mw := svc.RPCMiddleware(); mw != nil {
inters = append(inters, mw)
}

s.gsvr = grpc.NewServer(append(svc.RPCOptions(),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(inters...)))...)

s.gsvr.RegisterService(gdesc, svc)
}

Expand Down
15 changes: 13 additions & 2 deletions server/kit/kitserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"google.golang.org/grpc"

"github.com/NYTimes/gizmo/server/kit"
"github.com/NYTimes/gziphandler"
)

func TestKitServer(t *testing.T) {
Expand Down Expand Up @@ -84,11 +85,14 @@ func TestKitServer(t *testing.T) {
type server struct{}

func (s *server) Middleware(e endpoint.Endpoint) endpoint.Endpoint {
return e
return endpoint.Endpoint(func(ctx context.Context, r interface{}) (interface{}, error) {
kit.LogMsgWithFields(ctx, "kit middleware!")
return e(ctx, r)
})
}

func (s *server) HTTPMiddleware(h http.Handler) http.Handler {
return h
return gziphandler.GzipHandler(h)
}

func (s *server) HTTPOptions() []httptransport.ServerOption {
Expand All @@ -114,6 +118,13 @@ func (s *server) RPCServiceDesc() *grpc.ServiceDesc {
return &_KitTestService_serviceDesc
}

func (s *server) RPCMiddleware() grpc.UnaryServerInterceptor {
return grpc.UnaryServerInterceptor(func(ctx ocontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
kit.LogMsgWithFields(ctx, "rpc middleware!")
return handler(ctx, req)
})
}

func (s *server) RPCOptions() []grpc.ServerOption {
return nil
}
Expand Down
17 changes: 17 additions & 0 deletions server/kit/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,27 @@ type Service interface {
// }
HTTPEndpoints() map[string]map[string]HTTPEndpoint

// RPCMiddleware is for any service-wide gRPC specific middleware
// for easy integration with 3rd party grpc.UnaryServerInterceptors like
// http://godoc.org/cloud.google.com/go/trace#Client.GRPCServerInterceptor
//
// The underlying kit server already uses the one available grpc.UnaryInterceptor
// grpc.ServerOption so attempting to pass your own in this Service's RPCOptions()
// will cause a panic at startup.
//
// If you want to apply multiple RPC middlewares,
// we recommend using:
// http://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware#ChainUnaryServer
RPCMiddleware() grpc.UnaryServerInterceptor

// RPCServiceDesc allows services to declare an alternate gRPC
// representation of themselves to be hosted on the RPC_PORT (8081 by default).
RPCServiceDesc() *grpc.ServiceDesc

// RPCOptions are for service-wide gRPC server options.
//
// The underlying kit server already uses the one available grpc.UnaryInterceptor
// grpc.ServerOption so attempting to pass your own in this method will cause a panic
// at startup. We recommend using RPCMiddleware() to fill this need.
RPCOptions() []grpc.ServerOption
}

0 comments on commit 31968b2

Please sign in to comment.