node-api/tools.go

52 lines
904 B
Go
Raw Normal View History

2021-04-25 22:57:05 +00:00
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/nats-io/nats.go"
)
func errorReplyFormater(m *nats.Msg, message string, err error) error {
reply := ReplyMessage{
Error: true,
Payload: message,
}
log.Println("ERROR:", err.Error())
data, err := json.Marshal(reply)
if err != nil {
log.Println("ERROR:", err.Error())
return err
}
err = m.Respond(data)
if err != nil {
log.Println("ERROR:", err.Error())
return err
}
return err
}
func publish(appName string, state string, isErr bool) {
stateMessage := StateMessage{
AppName: appName,
Error: isErr,
Message: state,
}
data, err := stateMessage.JSON()
if err != nil {
log.Println("ERROR: publish:", err.Error())
}
subjectEvents := fmt.Sprintf("admin.apps.%s.states", config.NATSAlias)
err = nc.Publish(subjectEvents, data)
if err != nil {
log.Println("ERROR: publish:", err.Error())
}
}