grpc-ping/ping_server/main.go

49 lines
928 B
Go
Raw Normal View History

2021-07-07 15:13:36 +00:00
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
pb "lume-grpc/ping"
)
const (
port = ":50051"
)
type server struct {
pb.UnimplementedPingServer
}
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func (s *server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
log.Printf("Received: %v", req.Data)
return &pb.PingResponse{Data: Reverse(req.Data), Seq: req.Seq}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterPingServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}