Last active
February 1, 2019 17:37
-
-
Save Weiyuan-Lane/a7a3fd34c7316ebb4063e5a6b0967d76 to your computer and use it in GitHub Desktop.
Proposed Go kit usage across 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 ( | |
"context" | |
"google.golang.org/grpc" | |
"github.com/gorilla/mux" | |
"github.com/go-kit/kit/endpoint" | |
"github.com/exampleproject/httplogic" | |
"github.com/exampleproject/grpclogic" | |
gs "github.com/exampleproject/grpcschema" | |
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) { | |
// In here, you perform the required logic for the feature | |
// | |
// Required inputs for your logic can be obtained from the | |
// "request" parameter, typically through type assertion | |
// to a struct defined in your application | |
// | |
// Remember to return output that can be type asserted | |
// into a type that your gRPC and/or http response methods | |
// can act on. | |
} | |
} | |
// Mux http router variant to using go kit | |
func AddExampleToMuxRouter(router *mux.Router, method string, path string) { | |
httpHandler := httptransport.NewServer( | |
// Logic that is core of your application, and not coupled with | |
// communication protocol | |
makeCommonEndpoint(), | |
// A decode method for parsing input for http requests for your | |
// core logic | |
httplogic.DecodeExampleRequest, | |
// An encode method for creating responses destined for your http | |
// requests | |
// Output is also written on the "http.ResponseWriter" parameter here. | |
httplogic.EncodeExampleResponse, | |
) | |
// Interface your logic to a http router with method and path | |
router.Methods(method).Path(path).Handler(httpHandler) | |
} | |
// Not done within our application at this point, but integration of core logic | |
// is seamless as followed from http variant above | |
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) | |
} | |
type GrpcExampleWrapper struct { | |
exampleHandler grpctransport.Handler | |
} | |
func (g GrpcExampleWrapper) ExampleMethod (ctx context.Context, req *gs.ExampleRequest) (*gs.ExampleResponse, error) { | |
retctx, res, err := g.exampleHandler.ServeGRPC(ctx, req) | |
// Deal with err and processing the returned context | |
// ... | |
// Type assert for response from grpclogic.EncodeExampleResponse | |
// and return here | |
return res.(*gs.ExampleResponse), nil | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment