You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.5 KiB

  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. //go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto
  19. // Package main implements a server for Greeter service.
  20. package main
  21. import (
  22. "context"
  23. "log"
  24. "net"
  25. "google.golang.org/grpc"
  26. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  27. )
  28. const (
  29. port = ":50051"
  30. )
  31. // server is used to implement helloworld.GreeterServer.
  32. type server struct{}
  33. // SayHello implements helloworld.GreeterServer
  34. func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
  35. log.Printf("Received: %v", in.Name)
  36. return &pb.HelloReply{Message: "Hello " + in.Name}, nil
  37. }
  38. func main() {
  39. lis, err := net.Listen("tcp", port)
  40. if err != nil {
  41. log.Fatalf("failed to listen: %v", err)
  42. }
  43. s := grpc.NewServer()
  44. pb.RegisterGreeterServer(s, &server{})
  45. if err := s.Serve(lis); err != nil {
  46. log.Fatalf("failed to serve: %v", err)
  47. }
  48. }