2021-01-10 15:31:37 +08:00

48 lines
1002 B
Go

// grpc server
package main
import (
"context"
"net"
pb "github.com/esinio/geco/gen/service/echo/v1"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func main() {
s := grpc.NewServer()
pb.RegisterEchoServiceServer(s, &service{})
lis, err := net.Listen("tcp", ":9090")
if err != nil {
panic(err)
}
defer lis.Close()
defer s.GracefulStop()
if err := s.Serve(lis); err != nil {
panic(err)
}
}
// UnimplementedEchoServiceServer must be embedded to have forward compatible implementations.
type service struct {
pb.UnimplementedEchoServiceServer
}
func (s *service) Echo(ctx context.Context, req *pb.StringMessage) (*pb.StringMessage, error) {
if req.Value == "" {
return nil, status.Errorf(codes.Unimplemented, "value is empty")
}
return &pb.StringMessage{
Value: req.Value,
Timestamp: ptypes.TimestampNow(),
}, nil
}
// func (s *service) mustEmbedUnimplementedEchoServiceServer() {}