OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / examples / helloworld / greeter_server / main.go
1 /*
2  *
3  * Copyright 2015 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 //go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto
20
21 package main
22
23 import (
24         "log"
25         "net"
26
27         "golang.org/x/net/context"
28         "google.golang.org/grpc"
29         pb "google.golang.org/grpc/examples/helloworld/helloworld"
30         "google.golang.org/grpc/reflection"
31 )
32
33 const (
34         port = ":50051"
35 )
36
37 // server is used to implement helloworld.GreeterServer.
38 type server struct{}
39
40 // SayHello implements helloworld.GreeterServer
41 func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
42         return &pb.HelloReply{Message: "Hello " + in.Name}, nil
43 }
44
45 func main() {
46         lis, err := net.Listen("tcp", port)
47         if err != nil {
48                 log.Fatalf("failed to listen: %v", err)
49         }
50         s := grpc.NewServer()
51         pb.RegisterGreeterServer(s, &server{})
52         // Register reflection service on gRPC server.
53         reflection.Register(s)
54         if err := s.Serve(lis); err != nil {
55                 log.Fatalf("failed to serve: %v", err)
56         }
57 }