google.golang.org/grpc@v1.62.1/interop/server/server.go (about)

     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  
    19  // Binary server is an interop server.
    20  //
    21  // See interop test case descriptions [here].
    22  //
    23  // [here]: https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md
    24  package main
    25  
    26  import (
    27  	"flag"
    28  	"net"
    29  	"strconv"
    30  	"time"
    31  
    32  	"google.golang.org/grpc"
    33  	"google.golang.org/grpc/credentials"
    34  	"google.golang.org/grpc/credentials/alts"
    35  	"google.golang.org/grpc/grpclog"
    36  	"google.golang.org/grpc/internal"
    37  	"google.golang.org/grpc/interop"
    38  	"google.golang.org/grpc/orca"
    39  	"google.golang.org/grpc/testdata"
    40  
    41  	testgrpc "google.golang.org/grpc/interop/grpc_testing"
    42  )
    43  
    44  var (
    45  	useTLS     = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
    46  	useALTS    = flag.Bool("use_alts", false, "Connection uses ALTS if true (this option can only be used on GCP)")
    47  	altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
    48  	certFile   = flag.String("tls_cert_file", "", "The TLS cert file")
    49  	keyFile    = flag.String("tls_key_file", "", "The TLS key file")
    50  	port       = flag.Int("port", 10000, "The server port")
    51  
    52  	logger = grpclog.Component("interop")
    53  )
    54  
    55  func main() {
    56  	flag.Parse()
    57  	if *useTLS && *useALTS {
    58  		logger.Fatal("-use_tls and -use_alts cannot be both set to true")
    59  	}
    60  	p := strconv.Itoa(*port)
    61  	lis, err := net.Listen("tcp", ":"+p)
    62  	if err != nil {
    63  		logger.Fatalf("failed to listen: %v", err)
    64  	}
    65  	logger.Infof("interop server listening on %v", lis.Addr())
    66  	opts := []grpc.ServerOption{orca.CallMetricsServerOption(nil)}
    67  	if *useTLS {
    68  		if *certFile == "" {
    69  			*certFile = testdata.Path("server1.pem")
    70  		}
    71  		if *keyFile == "" {
    72  			*keyFile = testdata.Path("server1.key")
    73  		}
    74  		creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
    75  		if err != nil {
    76  			logger.Fatalf("Failed to generate credentials: %v", err)
    77  		}
    78  		opts = append(opts, grpc.Creds(creds))
    79  	} else if *useALTS {
    80  		altsOpts := alts.DefaultServerOptions()
    81  		if *altsHSAddr != "" {
    82  			altsOpts.HandshakerServiceAddress = *altsHSAddr
    83  		}
    84  		altsTC := alts.NewServerCreds(altsOpts)
    85  		opts = append(opts, grpc.Creds(altsTC))
    86  	}
    87  	server := grpc.NewServer(opts...)
    88  	metricsRecorder := orca.NewServerMetricsRecorder()
    89  	sopts := orca.ServiceOptions{
    90  		MinReportingInterval:  time.Second,
    91  		ServerMetricsProvider: metricsRecorder,
    92  	}
    93  	internal.ORCAAllowAnyMinReportingInterval.(func(*orca.ServiceOptions))(&sopts)
    94  	orca.Register(server, sopts)
    95  	testgrpc.RegisterTestServiceServer(server, interop.NewTestServer(interop.NewTestServerOptions{MetricsRecorder: metricsRecorder}))
    96  	server.Serve(lis)
    97  }