2020-10-03 12:40:19 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-04 01:19:13 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
pb "go-rpc/rpc"
|
|
|
|
"log"
|
2020-10-03 12:40:19 +02:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2020-10-04 01:19:13 +02:00
|
|
|
type NodeServer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NodeServer) GetNode(ctx context.Context, req *pb.NodeRequest) (*pb.NodeResponse, error) {
|
|
|
|
log.Println(" [x] Received request", req)
|
|
|
|
|
|
|
|
if req.Name == "special" {
|
|
|
|
return &pb.NodeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.NodeResponse{
|
|
|
|
Name: "Test Node",
|
|
|
|
Email: "test@no-reply.net",
|
|
|
|
Address: &pb.Address{
|
|
|
|
City: &pb.City{
|
|
|
|
Name: "Gliwice",
|
|
|
|
PostalCode: "44-122",
|
|
|
|
},
|
|
|
|
Street: "Testowa",
|
|
|
|
Number: 1,
|
|
|
|
Apartment: 1,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NodeServer) SayHello(ctx context.Context, req *pb.Message) (*pb.Message, error) {
|
|
|
|
return &pb.Message{
|
|
|
|
Content: "You said: " + req.Content,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
port := ":50500"
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(port))
|
2020-10-03 12:40:19 +02:00
|
|
|
if err != nil {
|
2020-10-04 01:19:13 +02:00
|
|
|
log.Fatalf(" [x] Failed to listen: %v\n", err)
|
2020-10-03 12:40:19 +02:00
|
|
|
}
|
2020-10-04 01:19:13 +02:00
|
|
|
log.Printf(" [x] Listening on %s\n", port)
|
|
|
|
s := grpc.NewServer()
|
|
|
|
pb.RegisterNodeServiceServer(s, &NodeServer{})
|
|
|
|
// reflection.Register(s)
|
|
|
|
s.Serve(lis)
|
2020-10-03 12:40:19 +02:00
|
|
|
}
|