Categorygithub.com/fuzzylabs/go-pubsub
modulepackage
0.0.0-20211020091826-02418264d680
Repository: https://github.com/fuzzylabs/go-pubsub.git
Documentation: pkg.go.dev

# README

PubSub

This library provides generic methods for decoding and publishing messages to Google Pub/Sub.

Usage

You can install the module directly from GitHub

go get -u github.com/fuzzylabs/go-pubsub@<version>

Where version can point to a commit hash or a branch, for example:

go get -u github.com/fuzzylabs/go-pubsub@9302e1d

or

go get -u github.com/fuzzylabs/go-pubsub@master

You can then import the library as follows:

import (
	hubspot "github.com/fuzzylabs/go-pubsub"
)

Examples

Decoding Push Subscription Messages

package main

import (
	pubsub "github.com/fuzzylabs/go-pubsub"
	"net/http"
)

func Entrypoint(w http.ResponseWriter, r *http.Request) {
	projectID := "test"
	pubsubApi, err := pubsub.NewPubSubDecoder()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	
	message, err := pubsubApi.DecodeData(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
}

Publishing Messages to Pub/Sub Topic

package main

import (
	pubsub "github.com/fuzzylabs/go-pubsub"
	"github.com/golang/protobuf/ptypes/empty"
)

func Entrypoint() {
	projectID := "test"
	pubsubApi, err := pubsub.NewPubSub(projectID)
	if err != nil {
		panic("")
	}
	
	// Replace with your message that satisfies `proto.Message` interface
	message := &empty.Empty{}
	pubsubApi.PublishMessage("test-topic", message)
}

Mocking

moq is used to generate mocks:

  • Mocks for external interfaces to use within unit tests
  • Mocks for go-pubsub API interfaces, for to make testing of applications that use the library easier
go generate

Testing

go vet
go test -coverprofile=coverage.out
go tool cover -html=coverage.out # To view test coverage

# Functions

NewPubSub returns a PubSub struct with a Pub/Sub client for a given Google Cloud project ID.
NewPubSubDecoder returns a PubSub struct without a Pub/Sub client, which is able to decode push-subscription messages.

# Structs

IPubSubMock is a mock implementation of IPubSub.
No description provided by the author
PubSub a struct that holds a Pub/Sub client for publishing.
PubSubClient implementation of a Pub/Sub client for publishing.
PubSubTopic implementation of a Pub/Sub topic for publishing.
No description provided by the author

# Interfaces

No description provided by the author
IPubSubClient interface for a Pub/Sub clienbt.
IPubSubPublishResult interface for results of publishing a message to Pub/Sub topic.
IPubSubTopic interface for a Pub/Sub topic (either existing or non-existing) for publishing.