mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 04:30:25 +00:00
60e256c267
Signed-off-by: dusan <borovcanindusan1@gmail.com>
52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package outputs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/absmach/supermq/pkg/messaging"
|
|
)
|
|
|
|
const protocol = "nats"
|
|
|
|
type ChannelPublisher struct {
|
|
RePubSub messaging.PubSub `json:"-"`
|
|
Channel string `json:"channel"`
|
|
Topic string `json:"topic"`
|
|
}
|
|
|
|
func (p *ChannelPublisher) Run(ctx context.Context, msg *messaging.Message, val any) error {
|
|
data, err := json.Marshal(val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m := &messaging.Message{
|
|
Domain: msg.Domain,
|
|
Publisher: msg.Publisher,
|
|
Created: msg.Created,
|
|
Channel: p.Channel,
|
|
Subtopic: p.Topic,
|
|
Protocol: protocol,
|
|
Payload: data,
|
|
}
|
|
|
|
topic := messaging.EncodeTopicSuffix(msg.Domain, p.Channel, p.Topic)
|
|
if err := p.RePubSub.Publish(ctx, topic, m); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cp *ChannelPublisher) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(map[string]string{
|
|
"type": ChannelsType.String(),
|
|
"channel": cp.Channel,
|
|
"topic": cp.Topic,
|
|
})
|
|
}
|