github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/examples/helloworld/main.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"net"
    25  
    26  	pb "github.com/lastbackend/toolkit/examples/helloworld/gen"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  var (
    31  	port = flag.Int("port", 9000, "The server port")
    32  )
    33  
    34  // server is used to implement lastbackend.helloworld.GreeterServer.
    35  type server struct {
    36  	pb.UnimplementedGreeterServer
    37  }
    38  
    39  // SayHello implements lastbackend.helloworld.GreeterServer
    40  func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    41  	log.Printf("Received: %v", in.GetName())
    42  	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
    43  }
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
    49  	if err != nil {
    50  		log.Fatalf("failed to listen: %v", err)
    51  	}
    52  
    53  	s := grpc.NewServer()
    54  
    55  	pb.RegisterGreeterServer(s, &server{})
    56  
    57  	log.Printf("server listening at %v", lis.Addr())
    58  
    59  	if err := s.Serve(lis); err != nil {
    60  		log.Fatalf("failed to serve: %v", err)
    61  	}
    62  }