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