server.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. *
  3. * Copyright 2014 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 interop server.
  19. package main
  20. import (
  21. "flag"
  22. "net"
  23. "strconv"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials"
  26. "google.golang.org/grpc/credentials/alts"
  27. "google.golang.org/grpc/grpclog"
  28. "google.golang.org/grpc/interop"
  29. testpb "google.golang.org/grpc/interop/grpc_testing"
  30. "google.golang.org/grpc/testdata"
  31. )
  32. var (
  33. useTLS = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
  34. useALTS = flag.Bool("use_alts", false, "Connection uses ALTS if true (this option can only be used on GCP)")
  35. altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
  36. certFile = flag.String("tls_cert_file", "", "The TLS cert file")
  37. keyFile = flag.String("tls_key_file", "", "The TLS key file")
  38. port = flag.Int("port", 10000, "The server port")
  39. )
  40. func main() {
  41. flag.Parse()
  42. if *useTLS && *useALTS {
  43. grpclog.Fatalf("use_tls and use_alts cannot be both set to true")
  44. }
  45. p := strconv.Itoa(*port)
  46. lis, err := net.Listen("tcp", ":"+p)
  47. if err != nil {
  48. grpclog.Fatalf("failed to listen: %v", err)
  49. }
  50. var opts []grpc.ServerOption
  51. if *useTLS {
  52. if *certFile == "" {
  53. *certFile = testdata.Path("server1.pem")
  54. }
  55. if *keyFile == "" {
  56. *keyFile = testdata.Path("server1.key")
  57. }
  58. creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
  59. if err != nil {
  60. grpclog.Fatalf("Failed to generate credentials %v", err)
  61. }
  62. opts = append(opts, grpc.Creds(creds))
  63. } else if *useALTS {
  64. altsOpts := alts.DefaultServerOptions()
  65. if *altsHSAddr != "" {
  66. altsOpts.HandshakerServiceAddress = *altsHSAddr
  67. }
  68. altsTC := alts.NewServerCreds(altsOpts)
  69. opts = append(opts, grpc.Creds(altsTC))
  70. }
  71. server := grpc.NewServer(opts...)
  72. testpb.RegisterTestServiceServer(server, interop.NewTestServer())
  73. server.Serve(lis)
  74. }