|
| 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 | +// Package main implements a server for Greeter service. |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "log" |
| 25 | + "net" |
| 26 | + |
| 27 | + pb "github.com/aws-samples/grpc-examples/examples/helloworld/helloworld" |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/health" |
| 30 | + "google.golang.org/grpc/health/grpc_health_v1" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + port = ":50051" |
| 35 | +) |
| 36 | + |
| 37 | +// server is used to implement helloworld.GreeterServer. |
| 38 | +type server struct { |
| 39 | + pb.UnimplementedGreeterServer |
| 40 | +} |
| 41 | + |
| 42 | +// sayHello implements helloworld.GreeterServer.SayHello |
| 43 | +func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { |
| 44 | + log.Printf("Received: %v", in.GetName()) |
| 45 | + return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil |
| 46 | +} |
| 47 | + |
| 48 | +func main() { |
| 49 | + lis, err := net.Listen("tcp", port) |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("failed to listen: %v", err) |
| 52 | + } |
| 53 | + s := grpc.NewServer() |
| 54 | + pb.RegisterGreeterServer(s, &server{}) |
| 55 | + grpc_health_v1.RegisterHealthServer(s, health.NewServer()) |
| 56 | + if err := s.Serve(lis); err != nil { |
| 57 | + log.Fatalf("failed to serve: %v", err) |
| 58 | + } |
| 59 | +} |
0 commit comments