|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "vacation_app/trace" |
| 8 | + "vacation_app/messages" |
| 9 | + |
| 10 | + log "github.com/sirupsen/logrus" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/wework/grabbit/gbus" |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +var runFlightsgServiceCmd = &cobra.Command{ |
| 18 | + Use: "flights", |
| 19 | + Short: "Run the flights service", |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + svcName := "flights-service" |
| 22 | + closer, err := trace.CreatetJeagerTracer(svcName) |
| 23 | + |
| 24 | + if err != nil { |
| 25 | + log.Printf("Could not initialize jaeger tracer: %s", err.Error()) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + defer closer.Close() |
| 30 | + gb := createBus(svcName) |
| 31 | + |
| 32 | + gb.HandleMessage(messages.BookFlightsCmd{}, HandleBookFlightCommand) |
| 33 | + gb.HandleMessage(messages.CancelFlightsCmd{}, HandleCancelFlightCommand) |
| 34 | + |
| 35 | + gb.Start() |
| 36 | + defer gb.Shutdown() |
| 37 | + |
| 38 | + fmt.Print("Flights service running...press any key to exit...\n") |
| 39 | + reader := bufio.NewReader(os.Stdin) |
| 40 | + reader.ReadString('\n') |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func HandleBookFlightCommand(invocation gbus.Invocation, message *gbus.BusMessage) error { |
| 45 | + cmd := message.Payload.(*messages.BookFlightsCmd) |
| 46 | + invocation.Log().Infof("booking flight to %s", cmd.Destination) |
| 47 | + reply := gbus.NewBusMessage(messages.BookFlightsRsp{ |
| 48 | + Success: true, |
| 49 | + }) |
| 50 | + invocation.Reply(invocation.Ctx(), reply) |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func HandleCancelFlightCommand(invocation gbus.Invocation, message *gbus.BusMessage) error { |
| 56 | + cmd := message.Payload.(*messages.CancelFlightsCmd) |
| 57 | + invocation.Log().Infof("canceling flight to %s", cmd.Destination) |
| 58 | + reply := gbus.NewBusMessage(messages.CancelFlightsRsp{ |
| 59 | + Success: true, |
| 60 | + }) |
| 61 | + invocation.Reply(invocation.Ctx(), reply) |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
0 commit comments