github.com/moleculer-go/moleculer@v0.3.3/examples/math/math.service.go (about)

     1  package math
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/moleculer-go/moleculer"
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func ServiceSchema() moleculer.ServiceSchema {
    11  	return mathService
    12  }
    13  
    14  var mathService = moleculer.ServiceSchema{
    15  	Name: "math",
    16  	Actions: []moleculer.Action{
    17  		{
    18  			Name:    "add",
    19  			Handler: addAction,
    20  		},
    21  		{
    22  			Name:    "sub",
    23  			Handler: subAction,
    24  		},
    25  		{
    26  			Name:    "mult",
    27  			Handler: multAction,
    28  		},
    29  	},
    30  	Events: []moleculer.Event{
    31  		{
    32  			Name:    "math.add.called",
    33  			Handler: onAddEvent,
    34  		},
    35  		{
    36  			Name:    "math.sub.called",
    37  			Handler: onSubEvent,
    38  		},
    39  	},
    40  	//Mixins: []*Mixin(helloWorldSchema),
    41  	Created: func(service moleculer.ServiceSchema, logger *log.Entry) {
    42  		fmt.Println("math service created !")
    43  	},
    44  	Started: func(ctx moleculer.BrokerContext, service moleculer.ServiceSchema) {
    45  		fmt.Println("math service started !")
    46  	},
    47  	Stopped: func(ctx moleculer.BrokerContext, service moleculer.ServiceSchema) {
    48  		fmt.Println("math service stopped !")
    49  	},
    50  }
    51  
    52  func onAddEvent(ctx moleculer.Context, params moleculer.Payload) {
    53  	fmt.Printf("\n onAddEvent :\n")
    54  	printEventParams(params)
    55  }
    56  
    57  func onSubEvent(ctx moleculer.Context, params moleculer.Payload) {
    58  	fmt.Printf("\n onAddEvent :\n")
    59  	printEventParams(params)
    60  }
    61  
    62  func addAction(context moleculer.Context, params moleculer.Payload) interface{} {
    63  	context.Logger().Info("math service add action.")
    64  	a := params.Get("a").Int()
    65  	b := params.Get("b").Int()
    66  	result := a + b
    67  
    68  	context.Logger().Info("params -> a: ", a, "b: ", b, "result: ", result)
    69  
    70  	defer context.Emit("add.called", map[string]int{
    71  		"a":      a,
    72  		"b":      b,
    73  		"result": result,
    74  	})
    75  
    76  	return result
    77  }
    78  
    79  func multAction(context moleculer.Context, params moleculer.Payload) interface{} {
    80  	a := params.Get("a").Int()
    81  	b := params.Get("b").Int()
    82  	var result int
    83  
    84  	for i := 1; i <= b; i++ {
    85  		actionResult := <-context.Call(
    86  			"math.add",
    87  			map[string]interface{}{
    88  				"a": a,
    89  				"b": a,
    90  			})
    91  		result = result + actionResult.Int()
    92  	}
    93  
    94  	defer context.Emit("mult.called", map[string]int{
    95  		"a":      a,
    96  		"b":      b,
    97  		"result": result,
    98  	})
    99  
   100  	return result
   101  }
   102  
   103  func subAction(context moleculer.Context, params moleculer.Payload) interface{} {
   104  	a := params.Get("a").Int()
   105  	b := params.Get("b").Int()
   106  	result := a - b
   107  
   108  	defer context.Emit("sub.called", map[string]int{
   109  		"a":      a,
   110  		"b":      b,
   111  		"result": result,
   112  	})
   113  	return result
   114  }
   115  
   116  func printEventParams(params moleculer.Payload) {
   117  	fmt.Printf("a: ")
   118  	fmt.Printf(params.Get("a").String())
   119  	fmt.Printf("b: ")
   120  	fmt.Printf(params.Get("b").String())
   121  	fmt.Printf("result: ")
   122  	fmt.Printf(params.Get("result").String())
   123  }