-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,403 additions
and
594 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
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
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
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,78 @@ | ||
package brokers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"github.com/pkg/errors" | ||
"github.com/willdot/sendit/config" | ||
"github.com/willdot/sendit/service" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
// GooglePubSubPublisher is a publisher that can send messages to a Google Pub/Sub server | ||
type GooglePubSubPublisher struct { | ||
client *pubsub.Client | ||
} | ||
|
||
// NewGooglePubSubPublisher will create a connection to a Google Pub/Sub server. Shutdown on the returned publisher should be called | ||
// to close the connection once finished | ||
func NewGooglePubSubPublisher(cfg *config.Config) (*GooglePubSubPublisher, error) { | ||
options := make([]option.ClientOption, 0) | ||
if cfg.GooglePubSubCfg.DisableAuth { | ||
options = append(options, option.WithoutAuthentication()) | ||
} | ||
|
||
if cfg.URL != "" { | ||
options = append(options, option.WithEndpoint(cfg.URL)) | ||
} | ||
|
||
client, err := pubsub.NewClient(context.Background(), cfg.GooglePubSubCfg.ProjectID, options...) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to connect to Google Pub/Sub") | ||
} | ||
|
||
return &GooglePubSubPublisher{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// Shutdown will close the Google Pub/Sub connection | ||
func (p *GooglePubSubPublisher) Shutdown() { | ||
_ = p.client.Close() | ||
} | ||
|
||
// Send will send the provided message | ||
func (p *GooglePubSubPublisher) Send(destination string, msg service.Message) error { | ||
headers, err := convertGooglePubSubHeaders(msg.Headers) | ||
if err != nil { | ||
return err | ||
} | ||
t := p.client.Topic(destination) | ||
res := t.Publish(context.Background(), &pubsub.Message{ | ||
Data: msg.Body, | ||
Attributes: headers, | ||
}) | ||
|
||
_, err = res.Get(context.Background()) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to publish message") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func convertGooglePubSubHeaders(headerData []byte) (map[string]string, error) { | ||
if headerData == nil { | ||
return nil, nil | ||
} | ||
|
||
var headers map[string]string | ||
err := json.Unmarshal(headerData, &headers) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to convert header data") | ||
} | ||
|
||
return headers, 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
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,47 @@ | ||
package brokers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-redis/redis/v8" | ||
"github.com/pkg/errors" | ||
"github.com/willdot/sendit/config" | ||
"github.com/willdot/sendit/service" | ||
) | ||
|
||
// RedisPublisher is a publisher that can send messages to a Redis server | ||
type RedisPublisher struct { | ||
client *redis.Client | ||
} | ||
|
||
// NewRedisPublisher will create a connection to a Redis server. Shutdown on the returned publisher should be called | ||
// to close the connection once finished | ||
func NewRedisPublisher(cfg *config.Config) (*RedisPublisher, error) { | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: cfg.URL, | ||
}) | ||
|
||
err := client.Ping(context.Background()).Err() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to connect to Redis") | ||
} | ||
|
||
return &RedisPublisher{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// Shutdown will close the Redis connection | ||
func (p *RedisPublisher) Shutdown() { | ||
_ = p.client.Shutdown(context.Background()) | ||
} | ||
|
||
// Send will send the provided message | ||
func (p *RedisPublisher) Send(destination string, msg service.Message) error { | ||
err := p.client.Publish(context.Background(), destination, msg.Body).Err() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to send message") | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.