-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Publisher and messaging types
Signed-off-by: Fabrizio Sestito <[email protected]>
- Loading branch information
1 parent
3f990ca
commit 1dfa442
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |