/
grpc.go
116 lines (95 loc) · 3.04 KB
/
grpc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package transport
import (
"context"
"github.com/facily-tech/go-scaffold/pkg/domains/quote"
pb "github.com/facily-tech/proto-examples/go-scaffold/build/go/quote"
grpctransport "github.com/go-kit/kit/transport/grpc"
"github.com/google/uuid"
)
type grpcServer struct {
findByID, upsert, delete grpctransport.Handler
pb.UnimplementedQuoteServiceServer
}
func NewGRPCServer(svc quote.ServiceI) pb.QuoteServiceServer {
return &grpcServer{
findByID: grpctransport.NewServer(
quote.FindByID(svc),
decodeGRPCFindByIDRequest,
encodeGRPCFindByIDResponse,
),
upsert: grpctransport.NewServer(
quote.Upsert(svc),
decodeGRPCUpsertRequest,
encodeGRPCUpsertResponse,
),
delete: grpctransport.NewServer(
quote.DeleteByID(svc),
decodeGRPCDeleteRequest,
encodeGRPCDeleteResponse,
),
}
}
func (s *grpcServer) FindByID(ctx context.Context, req *pb.FindRequest) (*pb.FindResponse, error) {
_, rep, err := s.findByID.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
findResp := rep.(pb.FindResponse) //nolint:govet
resp := &pb.FindResponse{Id: findResp.Id, Content: findResp.Content, Error: findResp.Error}
return resp, nil
}
func (s *grpcServer) Upsert(ctx context.Context, req *pb.UpsertRequest) (*pb.UpsertResponse, error) {
_, rep, err := s.upsert.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
jsonResp := rep.(quote.JSONResponse)
resp := &pb.UpsertResponse{Id: jsonResp.ID.String(), Content: jsonResp.Content, Error: jsonResp.Error}
return resp, nil
}
func (s *grpcServer) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.Error, error) {
_, _, err := s.delete.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return &pb.Error{}, nil
}
func decodeGRPCFindByIDRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*pb.FindRequest)
id, err := uuid.Parse(req.Id)
if err != nil {
return nil, err
}
return quote.FindByIDRequest{ID: id}, nil
}
func encodeGRPCFindByIDResponse(_ context.Context, grpcResp interface{}) (interface{}, error) {
resp := grpcResp.(quote.JSONResponse)
return pb.FindResponse{Id: resp.ID.String(), Content: resp.Content}, nil
}
func decodeGRPCUpsertRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*pb.UpsertRequest)
var id *uuid.UUID
if len(req.Id) > 0 {
pid, err := uuid.Parse(req.Id)
if err != nil {
return nil, err
}
id = &pid
}
return quote.UpsertRequest{JSONRequest: quote.JSONRequest{ID: id, Content: req.Content}}, nil
}
func encodeGRPCUpsertResponse(_ context.Context, grpcResp interface{}) (interface{}, error) {
resp := grpcResp.(quote.JSONResponse)
return quote.JSONResponse{ID: resp.ID, Content: resp.Content, Error: resp.Error}, nil
}
func decodeGRPCDeleteRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*pb.DeleteRequest)
id, err := uuid.Parse(req.Id)
if err != nil {
return nil, err
}
return quote.DeleteRequest{ID: id}, nil
}
func encodeGRPCDeleteResponse(_ context.Context, grpcResp interface{}) (interface{}, error) {
return nil, nil
}