-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
51 lines (43 loc) · 1.22 KB
/
receiver.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
package receiver
import (
"context"
"fmt"
"os"
"cloud.google.com/go/pubsub"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
// Subscriber provides pubsub subscriber.
type Subscriber interface {
Subscribe(ctx context.Context, subscription string, fn func(context.Context, []byte) bool) error
}
type receiver struct {
cli *pubsub.Client
}
// New returns Subscriber.
func New(ctx context.Context) (Subscriber, error) {
var cred *google.Credentials
var err error
if data := os.Getenv("SERVICE_ACCOUNT_JSON"); data != "" {
cred, err = google.CredentialsFromJSON(ctx, []byte(data), pubsub.ScopePubSub)
} else {
cred, err = google.FindDefaultCredentials(ctx, pubsub.ScopePubSub)
}
if err != nil {
return nil, fmt.Errorf("credential error: %w", err)
}
cli, err := pubsub.NewClient(ctx, cred.ProjectID, option.WithCredentials(cred))
if err != nil {
return nil, fmt.Errorf("pubsub new client error: %w", err)
}
return &receiver{
cli: cli,
}, nil
}
func (r *receiver) Subscribe(ctx context.Context, subscription string, fn func(context.Context, []byte) bool) error {
return r.cli.Subscription(subscription).Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
if fn(ctx, m.Data) {
m.Ack()
}
})
}