Merge pull request #221 from jupp0r/feature/addsvc-grpc-server
Use transport.grpc in addsvc example server
Peter Bourgon
8 years ago
8 | 8 | |
9 | 9 | func makeSumEndpoint(svc server.AddService) endpoint.Endpoint { |
10 | 10 | return func(ctx context.Context, request interface{}) (interface{}, error) { |
11 | req := request.(server.SumRequest) | |
11 | req := request.(*server.SumRequest) | |
12 | 12 | v := svc.Sum(req.A, req.B) |
13 | 13 | return server.SumResponse{V: v}, nil |
14 | 14 | } |
16 | 16 | |
17 | 17 | func makeConcatEndpoint(svc server.AddService) endpoint.Endpoint { |
18 | 18 | return func(ctx context.Context, request interface{}) (interface{}, error) { |
19 | req := request.(server.ConcatRequest) | |
19 | req := request.(*server.ConcatRequest) | |
20 | 20 | v := svc.Concat(req.A, req.B) |
21 | 21 | return server.ConcatResponse{V: v}, nil |
22 | 22 | } |
4 | 4 | |
5 | 5 | "github.com/go-kit/kit/examples/addsvc/pb" |
6 | 6 | "github.com/go-kit/kit/examples/addsvc/server" |
7 | servergrpc "github.com/go-kit/kit/examples/addsvc/server/grpc" | |
8 | "github.com/go-kit/kit/transport/grpc" | |
7 | 9 | ) |
8 | 10 | |
9 | 11 | type grpcBinding struct { |
10 | server.AddService | |
12 | sum, concat grpc.Handler | |
13 | } | |
14 | ||
15 | func newGRPCBinding(ctx context.Context, svc server.AddService) grpcBinding { | |
16 | return grpcBinding{ | |
17 | sum: grpc.NewServer(ctx, makeSumEndpoint(svc), servergrpc.DecodeSumRequest, servergrpc.EncodeSumResponse), | |
18 | concat: grpc.NewServer(ctx, makeConcatEndpoint(svc), servergrpc.DecodeConcatRequest, servergrpc.EncodeConcatResponse), | |
19 | } | |
11 | 20 | } |
12 | 21 | |
13 | 22 | func (b grpcBinding) Sum(ctx context.Context, req *pb.SumRequest) (*pb.SumReply, error) { |
14 | return &pb.SumReply{V: int64(b.AddService.Sum(int(req.A), int(req.B)))}, nil | |
23 | _, resp, err := b.sum.ServeGRPC(ctx, req) | |
24 | return resp.(*pb.SumReply), err | |
15 | 25 | } |
16 | 26 | |
17 | 27 | func (b grpcBinding) Concat(ctx context.Context, req *pb.ConcatRequest) (*pb.ConcatReply, error) { |
18 | return &pb.ConcatReply{V: b.AddService.Concat(req.A, req.B)}, nil | |
28 | _, resp, err := b.concat.ServeGRPC(ctx, req) | |
29 | return resp.(*pb.ConcatReply), err | |
19 | 30 | } |
172 | 172 | return |
173 | 173 | } |
174 | 174 | s := grpc.NewServer() // uses its own, internal context |
175 | pb.RegisterAddServer(s, grpcBinding{svc}) | |
175 | pb.RegisterAddServer(s, newGRPCBinding(root, svc)) | |
176 | 176 | transportLogger.Log("addr", *grpcAddr) |
177 | 177 | errc <- s.Serve(ln) |
178 | 178 | }() |
0 | package grpc | |
1 | ||
2 | import ( | |
3 | "golang.org/x/net/context" | |
4 | ||
5 | "github.com/go-kit/kit/examples/addsvc/pb" | |
6 | "github.com/go-kit/kit/examples/addsvc/server" | |
7 | ) | |
8 | ||
9 | func DecodeSumRequest(ctx context.Context, req interface{}) (interface{}, error) { | |
10 | sumRequest := req.(*pb.SumRequest) | |
11 | ||
12 | return &server.SumRequest{ | |
13 | A: int(sumRequest.A), | |
14 | B: int(sumRequest.B), | |
15 | }, nil | |
16 | } | |
17 | ||
18 | func DecodeConcatRequest(ctx context.Context, req interface{}) (interface{}, error) { | |
19 | concatRequest := req.(*pb.ConcatRequest) | |
20 | ||
21 | return &server.ConcatRequest{ | |
22 | A: concatRequest.A, | |
23 | B: concatRequest.B, | |
24 | }, nil | |
25 | } | |
26 | ||
27 | func EncodeSumResponse(ctx context.Context, resp interface{}) (interface{}, error) { | |
28 | domainResponse := resp.(server.SumResponse) | |
29 | ||
30 | return &pb.SumReply{ | |
31 | V: int64(domainResponse.V), | |
32 | }, nil | |
33 | } | |
34 | ||
35 | func EncodeConcatResponse(ctx context.Context, resp interface{}) (interface{}, error) { | |
36 | domainResponse := resp.(server.ConcatResponse) | |
37 | ||
38 | return &pb.ConcatReply{ | |
39 | V: domainResponse.V, | |
40 | }, nil | |
41 | } |