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.
 
 
 

65 lines
1.9 KiB

  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. "fmt"
  23. "log"
  24. "net"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/codes"
  27. ecpb "google.golang.org/grpc/examples/features/proto/echo"
  28. "google.golang.org/grpc/status"
  29. )
  30. const addr = "localhost:50051"
  31. type ecServer struct {
  32. addr string
  33. }
  34. func (s *ecServer) UnaryEcho(ctx context.Context, req *ecpb.EchoRequest) (*ecpb.EchoResponse, error) {
  35. return &ecpb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil
  36. }
  37. func (s *ecServer) ServerStreamingEcho(*ecpb.EchoRequest, ecpb.Echo_ServerStreamingEchoServer) error {
  38. return status.Errorf(codes.Unimplemented, "not implemented")
  39. }
  40. func (s *ecServer) ClientStreamingEcho(ecpb.Echo_ClientStreamingEchoServer) error {
  41. return status.Errorf(codes.Unimplemented, "not implemented")
  42. }
  43. func (s *ecServer) BidirectionalStreamingEcho(ecpb.Echo_BidirectionalStreamingEchoServer) error {
  44. return status.Errorf(codes.Unimplemented, "not implemented")
  45. }
  46. func main() {
  47. lis, err := net.Listen("tcp", addr)
  48. if err != nil {
  49. log.Fatalf("failed to listen: %v", err)
  50. }
  51. s := grpc.NewServer()
  52. ecpb.RegisterEchoServer(s, &ecServer{addr: addr})
  53. log.Printf("serving on %s\n", addr)
  54. if err := s.Serve(lis); err != nil {
  55. log.Fatalf("failed to serve: %v", err)
  56. }
  57. }