/
publisher.go
74 lines (62 loc) · 1.62 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pram
import (
"context"
"encoding/base64"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sns"
"google.golang.org/protobuf/proto"
)
type (
// Publisher represents a publisher
Publisher struct {
client SNS
topicARNFn func(context.Context, proto.Message) (string, error)
}
// PublisherOptions represents a set of publisher options
PublisherOptions struct {
TopicARNFn func(context.Context, proto.Message) (string, error)
}
)
// NewPublisher returns a new publisher
func NewPublisher(client SNS, optFns ...func(*PublisherOptions)) *Publisher {
o := PublisherOptions{
TopicARNFn: func(context.Context, proto.Message) (string, error) {
return "", errors.New("topic not found")
},
}
for _, fn := range optFns {
fn(&o)
}
return &Publisher{
client: client,
topicARNFn: o.TopicARNFn,
}
}
// Publish publishes the specified message
func (p *Publisher) Publish(ctx context.Context, m proto.Message, opts ...func(*Metadata)) error {
b, err := Marshal(m, opts...)
if err != nil {
return err
}
arn, err := p.topicARNFn(ctx, m)
if err != nil {
return err
}
res, err := p.client.Publish(ctx, &sns.PublishInput{
TopicArn: aws.String(arn),
Message: aws.String(base64.StdEncoding.EncodeToString(b)),
})
if err != nil {
return err
}
Logf("published %s to %s", *res.MessageId, arn)
return nil
}
// WithTopicRegistry configures the subscriber to use the specified registry
// to resolve topics, creating them if they do not exist
func WithTopicRegistry(r *Registry) func(*PublisherOptions) {
return func(o *PublisherOptions) {
o.TopicARNFn = r.TopicARN
}
}