Regenerate boilerplate

This commit is contained in:
Manfred Touron
2016-12-25 19:55:34 +01:00
parent f5d97d64c7
commit 91e9114c24
12 changed files with 291 additions and 54 deletions

View File

@@ -0,0 +1,69 @@
package user_clientgrpc
import (
jwt "github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
grpctransport "github.com/go-kit/kit/transport/grpc"
context "golang.org/x/net/context"
"google.golang.org/grpc"
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
)
func New(conn *grpc.ClientConn, logger log.Logger) pb.UserServiceServer {
var createuserEndpoint endpoint.Endpoint
{
createuserEndpoint = grpctransport.NewClient(
conn,
"user.UserService",
"CreateUser",
EncodeCreateUserRequest,
DecodeCreateUserResponse,
pb.CreateUserResponse{},
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
).Endpoint()
}
var getuserEndpoint endpoint.Endpoint
{
getuserEndpoint = grpctransport.NewClient(
conn,
"user.UserService",
"GetUser",
EncodeGetUserRequest,
DecodeGetUserResponse,
pb.GetUserResponse{},
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
).Endpoint()
}
return &endpoints.Endpoints{
CreateUserEndpoint: createuserEndpoint,
GetUserEndpoint: getuserEndpoint,
}
}
func EncodeCreateUserRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.CreateUserRequest)
return req, nil
}
func DecodeCreateUserResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
response := grpcResponse.(*pb.CreateUserResponse)
return response, nil
}
func EncodeGetUserRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.GetUserRequest)
return req, nil
}
func DecodeGetUserResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
response := grpcResponse.(*pb.GetUserResponse)
return response, nil
}

View File

@@ -10,6 +10,8 @@ import (
var _ = fmt.Errorf
type StreamEndpoint func(server interface{}, req interface{}) (err error)
type Endpoints struct {
CreateUserEndpoint endpoint.Endpoint

View File

@@ -4,9 +4,10 @@ import (
"fmt"
grpctransport "github.com/go-kit/kit/transport/grpc"
context "golang.org/x/net/context"
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
context "golang.org/x/net/context"
)
// avoid import errors
@@ -19,7 +20,7 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserS
createuser: grpctransport.NewServer(
ctx,
endpoints.CreateUserEndpoint,
decodeCreateUserRequest,
decodeRequest,
encodeCreateUserResponse,
options...,
),
@@ -27,7 +28,7 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserS
getuser: grpctransport.NewServer(
ctx,
endpoints.GetUserEndpoint,
decodeGetUserRequest,
decodeRequest,
encodeGetUserResponse,
options...,
),
@@ -48,10 +49,6 @@ func (s *grpcServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest)
return rep.(*pb.CreateUserResponse), nil
}
func decodeCreateUserRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
return grpcReq, nil
}
func encodeCreateUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
resp := response.(*pb.CreateUserResponse)
return resp, nil
@@ -65,11 +62,26 @@ func (s *grpcServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.G
return rep.(*pb.GetUserResponse), nil
}
func decodeGetUserRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
return grpcReq, nil
}
func encodeGetUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
resp := response.(*pb.GetUserResponse)
return resp, nil
}
func decodeRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
return grpcReq, nil
}
type streamHandler interface {
Do(server interface{}, req interface{}) (err error)
}
type server struct {
e endpoints.StreamEndpoint
}
func (s server) Do(server interface{}, req interface{}) (err error) {
if err := s.e(server, req); err != nil {
return err
}
return nil
}

View File

@@ -17,7 +17,7 @@ func MakeCreateUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoi
ctx,
endpoint,
decodeCreateUserRequest,
encodeCreateUserResponse,
encodeResponse,
[]httptransport.ServerOption{}...,
)
}
@@ -30,16 +30,12 @@ func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{},
return &req, nil
}
func encodeCreateUserResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
return httptransport.NewServer(
ctx,
endpoint,
decodeGetUserRequest,
encodeGetUserResponse,
encodeResponse,
[]httptransport.ServerOption{}...,
)
}
@@ -52,7 +48,7 @@ func decodeGetUserRequest(ctx context.Context, r *http.Request) (interface{}, er
return &req, nil
}
func encodeGetUserResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}