-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
endpoints.go
66 lines (53 loc) · 1.73 KB
/
endpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package user_endpoints
import (
"fmt"
"github.com/go-kit/kit/endpoint"
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
context "golang.org/x/net/context"
)
var _ = fmt.Errorf
type StreamEndpoint func(server interface{}, req interface{}) (err error)
type Endpoints struct {
CreateUserEndpoint endpoint.Endpoint
GetUserEndpoint endpoint.Endpoint
}
func (e *Endpoints) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
out, err := e.CreateUserEndpoint(ctx, in)
if err != nil {
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
}
return out.(*pb.CreateUserResponse), err
}
func (e *Endpoints) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
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),
}
}