-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_grpc_test.go
46 lines (38 loc) · 1.05 KB
/
server_grpc_test.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
package luchen_test
import (
"context"
grpctransport "github.com/go-kit/kit/transport/grpc"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"github.com/fengjx/luchen"
)
func newHelloGRPCServer(serviceName, addr string) *luchen.GRPCServer {
server := luchen.NewGRPCServer(
luchen.WithServiceName(serviceName),
luchen.WithServerAddr(addr),
)
server.RegisterService(func(grpcServer *grpc.Server) {
pb.RegisterGreeterServer(grpcServer, newGreeterServer())
})
return server
}
func newGreeterServer() pb.GreeterServer {
svr := &GreeterServer{}
svr.sayHello = luchen.NewGRPCTransportServer(
makeSayHelloEndpoint(),
luchen.DecodePB[pb.HelloRequest],
luchen.EncodePB[pb.HelloReply],
)
return svr
}
type GreeterServer struct {
pb.UnimplementedGreeterServer
sayHello grpctransport.Handler
}
func (s *GreeterServer) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
_, resp, err := s.sayHello.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return resp.(*pb.HelloReply), nil
}