Skip to content

Commit

Permalink
feat: add Publisher and messaging types
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito committed Oct 8, 2024
1 parent 3f990ca commit 1dfa442
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/messaging/publisher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package messaging

import (
"encoding/json"
"fmt"

"github.com/nats-io/nats.go"
)

const MessageTypeHeader = "MessageType"

type Publisher interface {
Publish(message Message) error
}

type publisher struct {
js nats.JetStreamContext
}

func NewPublisher(js nats.JetStreamContext) Publisher {
return &publisher{
js: js,
}
}

func (p *publisher) Publish(message Message) error {
data, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("failed to marshal message to JSON: %w", err)
}

header := make(nats.Header)
header.Add(MessageTypeHeader, message.GetType())

msg := &nats.Msg{
Subject: SbombasticSubject,
Data: data,
Header: header,
}

if _, err := p.js.PublishMsg(msg); err != nil {
return fmt.Errorf("failed to publish message: %w", err)
}

return nil
}
22 changes: 22 additions & 0 deletions internal/messaging/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package messaging

type Message interface {
GetType() string
}

type BaseMessage struct {
Type string `json:"type"`
}

type CreateCatalog struct {
BaseMessage
Name string `json:"name"`
URL string `json:"url"`
Repositories []string `json:"repositories"`
CABundle string `json:"caBundle"`
Insecure bool `json:"insecure"`
}

func (m *CreateCatalog) GetType() string {
return "CreateRegistryCatalog"
}

0 comments on commit 1dfa442

Please sign in to comment.