From f5d97d64c7340f6a15ff7e0688045ab07293ca89 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sun, 25 Dec 2016 19:50:30 +0100 Subject: [PATCH] Add go-kit client --- .../gen/client/grpc/client.go.tmpl | 56 +++++++++++++++++++ .../gen/transports/grpc/grpc.go.tmpl | 3 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 examples/go-kit/templates/{{.File.Package}}/gen/client/grpc/client.go.tmpl diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/client/grpc/client.go.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/client/grpc/client.go.tmpl new file mode 100644 index 0000000..e3813c9 --- /dev/null +++ b/examples/go-kit/templates/{{.File.Package}}/gen/client/grpc/client.go.tmpl @@ -0,0 +1,56 @@ +package {{.File.Package}}_clientgrpc + +import ( + "github.com/go-kit/kit/log" + context "golang.org/x/net/context" + "google.golang.org/grpc" + grpctransport "github.com/go-kit/kit/transport/grpc" + "github.com/go-kit/kit/endpoint" + jwt "github.com/go-kit/kit/auth/jwt" + + pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb" + endpoints "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/endpoints" +) + +{{$file:=.File}} + +func New(conn *grpc.ClientConn, logger log.Logger) pb.{{.File.Package | title}}ServiceServer { + {{range .Service.Method}} + {{if and (not .ServerStreaming) (not .ClientStreaming)}} + var {{.Name | lower}}Endpoint endpoint.Endpoint + { + {{.Name | lower}}Endpoint = grpctransport.NewClient( + conn, + "{{$file.Package}}.{{$file.Package | title}}Service", + "{{.Name}}", + Encode{{.Name}}Request, + Decode{{.Name}}Response, + pb.{{.Name}}Response{}, + append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))..., + ).Endpoint() + } + {{end}} + {{end}} + + return &endpoints.Endpoints { + {{range .Service.Method}} + {{if and (not .ServerStreaming) (not .ClientStreaming)}} + {{.Name | title}}Endpoint: {{.Name | lower}}Endpoint, + {{end}} + {{end}} + } +} + +{{range .Service.Method}} + {{if and (not .ServerStreaming) (not .ClientStreaming)}} + func Encode{{.Name}}Request(_ context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.{{.Name}}Request) + return req, nil + } + + func Decode{{.Name}}Response(_ context.Context, grpcResponse interface{}) (interface{}, error) { + response := grpcResponse.(*pb.{{.Name}}Response) + return response, nil + } + {{end}} +{{end}} diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl index 260fec5..4116453 100644 --- a/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl +++ b/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl @@ -6,9 +6,10 @@ import ( "fmt" context "golang.org/x/net/context" + grpctransport "github.com/go-kit/kit/transport/grpc" + pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb" endpoints "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/endpoints" - grpctransport "github.com/go-kit/kit/transport/grpc" ) // avoid import errors