github.com/harvey-h-yu/prototool@v1.3.0/example/cmd/excited/main.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"io"
    26  	"log"
    27  	"net"
    28  
    29  	"github.com/uber/prototool/example/gen/proto/go/foo"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  func main() {
    34  	if err := do(); err != nil {
    35  		log.Fatal(err.Error())
    36  	}
    37  }
    38  
    39  func do() error {
    40  	grpcServer := grpc.NewServer()
    41  	foopb.RegisterExcitedServiceServer(grpcServer, newServer())
    42  	listener, err := net.Listen("tcp", "0.0.0.0:8080")
    43  	if err != nil {
    44  		return err
    45  	}
    46  	return grpcServer.Serve(listener)
    47  }
    48  
    49  type server struct{}
    50  
    51  func newServer() *server {
    52  	return &server{}
    53  }
    54  
    55  func (s *server) Exclamation(ctx context.Context, request *foopb.ExclamationRequest) (*foopb.ExclamationResponse, error) {
    56  	return &foopb.ExclamationResponse{
    57  		Value: request.Value + "!",
    58  	}, nil
    59  }
    60  
    61  func (s *server) ExclamationClientStream(streamServer foopb.ExcitedService_ExclamationClientStreamServer) error {
    62  	value := ""
    63  	for request, err := streamServer.Recv(); err != io.EOF; request, err = streamServer.Recv() {
    64  		if err != nil {
    65  			return err
    66  		}
    67  		value += request.Value
    68  	}
    69  	return streamServer.SendAndClose(&foopb.ExclamationResponse{
    70  		Value: value + "!",
    71  	})
    72  }
    73  
    74  func (s *server) ExclamationServerStream(request *foopb.ExclamationRequest, streamServer foopb.ExcitedService_ExclamationServerStreamServer) error {
    75  	for _, c := range request.Value {
    76  		if err := streamServer.Send(&foopb.ExclamationResponse{
    77  			Value: string(c),
    78  		}); err != nil {
    79  			return err
    80  		}
    81  	}
    82  	return streamServer.Send(&foopb.ExclamationResponse{
    83  		Value: "!",
    84  	})
    85  }
    86  
    87  func (s *server) ExclamationBidiStream(streamServer foopb.ExcitedService_ExclamationBidiStreamServer) error {
    88  	for request, err := streamServer.Recv(); err != io.EOF; request, err = streamServer.Recv() {
    89  		if err != nil {
    90  			return err
    91  		}
    92  		if err := streamServer.Send(&foopb.ExclamationResponse{
    93  			Value: request.Value + "!",
    94  		}); err != nil {
    95  			return err
    96  		}
    97  	}
    98  	return nil
    99  }