/
server_grpc.go
87 lines (73 loc) · 2.48 KB
/
server_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
package vault
import (
"golang.org/x/net/context"
grpctransport "github.com/go-kit/kit/transport/grpc"
"github.com/matryer/goblueprints/chapter10/vault/pb"
)
// NewGRPCServer gets a new pb.VaultServer.
func NewGRPCServer(ctx context.Context, endpoints Endpoints) pb.VaultServer {
return &grpcServer{
hash: grpctransport.NewServer(
ctx,
endpoints.HashEndpoint,
DecodeGRPCHashRequest,
EncodeGRPCHashResponse,
),
validate: grpctransport.NewServer(
ctx,
endpoints.ValidateEndpoint,
DecodeGRPCValidateRequest,
EncodeGRPCValidateResponse,
),
}
}
type grpcServer struct {
hash grpctransport.Handler
validate grpctransport.Handler
}
func (s *grpcServer) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
_, resp, err := s.hash.ServeGRPC(ctx, r)
if err != nil {
return nil, err
}
return resp.(*pb.HashResponse), nil
}
func (s *grpcServer) Validate(ctx context.Context, r *pb.ValidateRequest) (*pb.ValidateResponse, error) {
_, resp, err := s.validate.ServeGRPC(ctx, r)
if err != nil {
return nil, err
}
return resp.(*pb.ValidateResponse), nil
}
func EncodeGRPCHashRequest(ctx context.Context, r interface{}) (interface{}, error) {
req := r.(hashRequest)
return &pb.HashRequest{Password: req.Password}, nil
}
func DecodeGRPCHashRequest(ctx context.Context, r interface{}) (interface{}, error) {
req := r.(*pb.HashRequest)
return hashRequest{Password: req.Password}, nil
}
func EncodeGRPCHashResponse(ctx context.Context, r interface{}) (interface{}, error) {
res := r.(hashResponse)
return &pb.HashResponse{Hash: res.Hash, Err: res.Err}, nil
}
func DecodeGRPCHashResponse(ctx context.Context, r interface{}) (interface{}, error) {
res := r.(*pb.HashResponse)
return hashResponse{Hash: res.Hash, Err: res.Err}, nil
}
func EncodeGRPCValidateRequest(ctx context.Context, r interface{}) (interface{}, error) {
req := r.(validateRequest)
return &pb.ValidateRequest{Password: req.Password, Hash: req.Hash}, nil
}
func DecodeGRPCValidateRequest(ctx context.Context, r interface{}) (interface{}, error) {
req := r.(*pb.ValidateRequest)
return validateRequest{Password: req.Password, Hash: req.Hash}, nil
}
func EncodeGRPCValidateResponse(ctx context.Context, r interface{}) (interface{}, error) {
res := r.(validateResponse)
return &pb.ValidateResponse{Valid: res.Valid}, nil
}
func DecodeGRPCValidateResponse(ctx context.Context, r interface{}) (interface{}, error) {
res := r.(*pb.ValidateResponse)
return validateResponse{Valid: res.Valid}, nil
}