Last active
April 4, 2019 12:29
-
-
Save Weiyuan-Lane/64f1203b59150e53da5359b4da0da232 to your computer and use it in GitHub Desktop.
[Shortened] Proposed go-kit usage over different protocols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import ( | |
httptransport "github.com/go-kit/kit/transport/http" | |
grpctransport "github.com/go-kit/kit/transport/grpc" | |
) | |
func makeCommonEndpoint() endpoint.Endpoint { | |
return func(ctx context.Context, request interface{}) (interface{}, error) { | |
// Some Logic Here | |
} | |
} | |
// Mux http router variant to using go kit | |
func AddExampleToMuxRouter(router *mux.Router, method string, path string) { | |
httpHandler := httptransport.NewServer( | |
makeCommonEndpoint(), | |
httplogic.DecodeExampleRequest, | |
httplogic.EncodeExampleResponse, | |
) | |
router.Methods(method).Path(path).Handler(httpHandler) | |
} | |
// grpc variant using go kit | |
func AddExampleToGrpcServer(server *grpc.Server, serviceSpec *grpc.ServiceDesc) { | |
grpcHandler := grpctransport.NewServer( | |
makeCommonEndpoint(), | |
grpclogic.DecodeExampleRequest, | |
grpclogic.EncodeExampleResponse, | |
) | |
grpcWrapperPtr := &GrpcExampleWrapper{ | |
exampleHandler: grpcHandler, | |
}) | |
// serviceSpec will contain the method name of "ExampleMethod" | |
// for invoking as part of the gRPC request | |
server.RegisterService(serviceSpec, grpcWrapperPtr) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment