go-micro.dev/v5@v5.12.0/cmd/micro/cli/new/template/handler.go (about)

     1  package template
     2  
     3  var (
     4  	HandlerSRV = `package handler
     5  
     6  import (
     7  	"context"
     8  
     9  	log "go-micro.dev/v5/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  
    45  	SubscriberSRV = `package subscriber
    46  
    47  import (
    48  	"context"
    49  	log "go-micro.dev/v5/logger"
    50  
    51  	pb "{{.Dir}}/proto"
    52  )
    53  
    54  type {{title .Alias}} struct{}
    55  
    56  func (e *{{title .Alias}}) Handle(ctx context.Context, msg *pb.Message) error {
    57  	log.Info("Handler Received message: ", msg.Say)
    58  	return nil
    59  }
    60  
    61  func Handler(ctx context.Context, msg *pb.Message) error {
    62  	log.Info("Function Received message: ", msg.Say)
    63  	return nil
    64  }
    65  `
    66  )