main.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "google.golang.org/grpc/credentials/alts"
  29. ecpb "google.golang.org/grpc/examples/features/proto/echo"
  30. "google.golang.org/grpc/status"
  31. )
  32. var port = flag.Int("port", 50051, "the port to serve on")
  33. type ecServer struct{}
  34. func (s *ecServer) UnaryEcho(ctx context.Context, req *ecpb.EchoRequest) (*ecpb.EchoResponse, error) {
  35. return &ecpb.EchoResponse{Message: req.Message}, 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. flag.Parse()
  48. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
  49. if err != nil {
  50. log.Fatalf("failed to listen: %v", err)
  51. }
  52. // Create alts based credential.
  53. altsTC := alts.NewServerCreds(alts.DefaultServerOptions())
  54. s := grpc.NewServer(grpc.Creds(altsTC))
  55. // Register EchoServer on the server.
  56. ecpb.RegisterEchoServer(s, &ecServer{})
  57. if err := s.Serve(lis); err != nil {
  58. log.Fatalf("failed to serve: %v", err)
  59. }
  60. }