gobuch-trainingfellow/registrierung/nats/notifier.go

37 lines
948 B
Go
Raw Normal View History

2020-08-31 13:23:01 +00:00
package nats
import (
log "github.com/sirupsen/logrus"
"github.com/nats-io/nats.go"
"training-fellow.de/registrierung"
)
2020-10-23 14:15:43 +00:00
//NewNotifier erzeugt eine neue Instanz eines RegistrierungsNotifier für die Kommunikation mit NATS
func NewNotifier(url string) registrierung.RegistrierungsNotifier {
2020-08-31 13:23:01 +00:00
return &notifier{url}
}
type notifier struct {
url string
}
2020-10-23 14:15:43 +00:00
//InformAboutNewRegistrierung informiert über eine neue Registrierung
2020-08-31 13:23:01 +00:00
func (nn *notifier) InformAboutNewRegistrierung(registrierung *registrierung.Registrierung) error {
notifierLogger := log.WithField("Registrierung", registrierung)
notifierLogger.Info("Inform about new Registrierung")
nc, err := nats.Connect(nn.url)
if err != nil {
2020-10-21 05:42:27 +00:00
notifierLogger.WithError(err).Error("Could not connect to server: ")
2020-08-31 13:23:01 +00:00
return err
}
defer nc.Close()
c, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
defer c.Close()
return c.Publish("traingfellow.registrierung.neu", registrierung)
}