1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Drasko DRASKOVIC 2554adcc1e Use interface
Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
2017-09-24 17:19:42 +02:00

35 lines
873 B
Go

// Package nats contains NATS-specific message repository implementation.
package nats
import (
"encoding/json"
"github.com/mainflux/mainflux/writer"
broker "github.com/nats-io/go-nats"
)
const topic string = "normalizer.senml"
var _ writer.MessageRepository = (*natsRepository)(nil)
type natsRepository struct {
nc *broker.Conn
}
// NewMessageRepository instantiates NATS message repository. Note that the
// repository will not truly persist messages, but instead they will be
// published to the topic and made available for persisting by all interested
// parties, i.e. the message-writer service.
func NewMessageRepository(nc *broker.Conn) writer.MessageRepository {
return &natsRepository{nc}
}
func (repo *natsRepository) Save(msg writer.Message) error {
b, err := json.Marshal(msg)
if err != nil {
return err
}
return repo.nc.Publish(topic, b)
}