protoc-gen-go-micro/examples/go-kit/services/user/gen/endpoints/endpoints.go

70 lines
1.8 KiB
Go
Raw Normal View History

2016-12-13 19:19:04 +03:00
package user_endpoints
import (
2017-03-16 17:41:09 +03:00
context "context"
2016-12-13 19:19:04 +03:00
"fmt"
2016-12-13 19:19:04 +03:00
"github.com/go-kit/kit/endpoint"
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
2017-03-16 17:41:09 +03:00
oldcontext "golang.org/x/net/context"
2016-12-13 19:19:04 +03:00
)
2017-03-16 17:41:09 +03:00
var _ = endpoint.Chain
2016-12-13 19:19:04 +03:00
var _ = fmt.Errorf
2017-03-16 17:41:09 +03:00
var _ = context.Background
2016-12-13 19:19:04 +03:00
2016-12-25 21:55:34 +03:00
type StreamEndpoint func(server interface{}, req interface{}) (err error)
2016-12-13 19:19:04 +03:00
type Endpoints struct {
CreateUserEndpoint endpoint.Endpoint
GetUserEndpoint endpoint.Endpoint
}
2017-03-16 17:41:09 +03:00
func (e *Endpoints) CreateUser(ctx oldcontext.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
2016-12-13 19:19:04 +03:00
out, err := e.CreateUserEndpoint(ctx, in)
if err != nil {
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
}
return out.(*pb.CreateUserResponse), err
}
2017-03-16 17:41:09 +03:00
func (e *Endpoints) GetUser(ctx oldcontext.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
2016-12-13 19:19:04 +03:00
out, err := e.GetUserEndpoint(ctx, in)
if err != nil {
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
}
return out.(*pb.GetUserResponse), err
}
func MakeCreateUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.CreateUserRequest)
rep, err := svc.CreateUser(ctx, req)
if err != nil {
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
}
return rep, nil
}
}
func MakeGetUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.GetUserRequest)
rep, err := svc.GetUser(ctx, req)
if err != nil {
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
}
return rep, nil
}
}
func MakeEndpoints(svc pb.UserServiceServer) Endpoints {
return Endpoints{
CreateUserEndpoint: MakeCreateUserEndpoint(svc),
GetUserEndpoint: MakeGetUserEndpoint(svc),
}
}