49 lines
928 B
Go
49 lines
928 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
pb "grpc-ping/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)
|
|
}
|
|
}
|