main.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. *
  3. * Copyright 2018 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. // Binary server is an example server.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "net"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. pb "google.golang.org/grpc/examples/features/proto/echo"
  29. "google.golang.org/grpc/status"
  30. _ "google.golang.org/grpc/encoding/gzip" // Install the gzip compressor
  31. )
  32. var port = flag.Int("port", 50051, "the port to serve on")
  33. type server struct{}
  34. func (s *server) UnaryEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
  35. fmt.Printf("UnaryEcho called with message %q\n", in.GetMessage())
  36. return &pb.EchoResponse{Message: in.Message}, nil
  37. }
  38. func (s *server) ServerStreamingEcho(in *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
  39. return status.Error(codes.Unimplemented, "not implemented")
  40. }
  41. func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
  42. return status.Error(codes.Unimplemented, "not implemented")
  43. }
  44. func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
  45. return status.Error(codes.Unimplemented, "not implemented")
  46. }
  47. func main() {
  48. flag.Parse()
  49. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
  50. if err != nil {
  51. log.Fatalf("failed to listen: %v", err)
  52. }
  53. fmt.Printf("server listening at %v\n", lis.Addr())
  54. s := grpc.NewServer()
  55. pb.RegisterEchoServer(s, &server{})
  56. s.Serve(lis)
  57. }