OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / interop / server / server.go
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 package main
20
21 import (
22         "flag"
23         "net"
24         "strconv"
25
26         "google.golang.org/grpc"
27         "google.golang.org/grpc/credentials"
28         "google.golang.org/grpc/grpclog"
29         "google.golang.org/grpc/interop"
30         testpb "google.golang.org/grpc/interop/grpc_testing"
31         "google.golang.org/grpc/testdata"
32 )
33
34 var (
35         useTLS   = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
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
41 func main() {
42         flag.Parse()
43         p := strconv.Itoa(*port)
44         lis, err := net.Listen("tcp", ":"+p)
45         if err != nil {
46                 grpclog.Fatalf("failed to listen: %v", err)
47         }
48         var opts []grpc.ServerOption
49         if *useTLS {
50                 if *certFile == "" {
51                         *certFile = testdata.Path("server1.pem")
52                 }
53                 if *keyFile == "" {
54                         *keyFile = testdata.Path("server1.key")
55                 }
56                 creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
57                 if err != nil {
58                         grpclog.Fatalf("Failed to generate credentials %v", err)
59                 }
60                 opts = []grpc.ServerOption{grpc.Creds(creds)}
61         }
62         server := grpc.NewServer(opts...)
63         testpb.RegisterTestServiceServer(server, interop.NewTestServer())
64         server.Serve(lis)
65 }