github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/new/template/handler.go (about)

     1  package template
     2  
     3  var (
     4  	HandlerSRV = `package handler
     5  
     6  import (
     7  	"context"
     8  
     9  	log "github.com/tickoalcantara12/micro/v3/service/logger"
    10  
    11  	pb "{{.Dir}}/proto"
    12  )
    13  
    14  type {{title .Alias}} struct{}
    15  
    16  // Return a new handler
    17  func New() *{{title .Alias}} {
    18  	return &{{title .Alias}}{}
    19  }
    20  
    21  // Call is a single request handler called via client.Call or the generated client code
    22  func (e *{{title .Alias}}) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
    23  	log.Info("Received {{title .Alias}}.Call request")
    24  	rsp.Msg = "Hello " + req.Name
    25  	return nil
    26  }
    27  
    28  // Stream is a server side stream handler called via client.Stream or the generated client code
    29  func (e *{{title .Alias}}) Stream(ctx context.Context, req *pb.StreamingRequest, stream pb.{{title .Alias}}_StreamStream) error {
    30  	log.Infof("Received {{title .Alias}}.Stream request with count: %d", req.Count)
    31  
    32  	for i := 0; i < int(req.Count); i++ {
    33  		log.Infof("Responding: %d", i)
    34  		if err := stream.Send(&pb.StreamingResponse{
    35  			Count: int64(i),
    36  		}); err != nil {
    37  			return err
    38  		}
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  // PingPong is a bidirectional stream handler called via client.Stream or the generated client code
    45  func (e *{{title .Alias}}) PingPong(ctx context.Context, stream pb.{{title .Alias}}_PingPongStream) error {
    46  	for {
    47  		req, err := stream.Recv()
    48  		if err != nil {
    49  			return err
    50  		}
    51  		log.Infof("Got ping %v", req.Stroke)
    52  		if err := stream.Send(&pb.Pong{Stroke: req.Stroke}); err != nil {
    53  			return err
    54  		}
    55  	}
    56  }
    57  `
    58  
    59  	SubscriberSRV = `package subscriber
    60  
    61  import (
    62  	"context"
    63  	log "github.com/tickoalcantara12/micro/v3/service/logger"
    64  
    65  	pb "{{.Dir}}/proto"
    66  )
    67  
    68  type {{title .Alias}} struct{}
    69  
    70  func (e *{{title .Alias}}) Handle(ctx context.Context, msg *pb.Message) error {
    71  	log.Info("Handler Received message: ", msg.Say)
    72  	return nil
    73  }
    74  
    75  func Handler(ctx context.Context, msg *pb.Message) error {
    76  	log.Info("Function Received message: ", msg.Say)
    77  	return nil
    78  }
    79  `
    80  )