Documentation ¶
Index ¶
- Constants
- Variables
- func EnsureSubscriptionsExist(ctx context.Context, pubsubClient *pubsub.Client, ...) error
- func EnsureTopicsExist(ctx context.Context, pubsubClient *pubsub.Client, topicIDs []string) error
- func GetPubSubTopic(m *PubSubPayload) (string, error)
- func GetPushSubscriptionConfig(ctx context.Context, pubsubClient *pubsub.Client, topicID string, ...) (*pubsub.SubscriptionConfig, error)
- func GetServiceAccountEmail() (string, error)
- func NamespacePubsubIdentifier(serviceName string, topicID string, environment string, version string) string
- func PublishToPubsub(ctx context.Context, pubsubClient *pubsub.Client, topicID string, ...) error
- func ReverseSubscriptionIDs(topicIDs []string, environment string, serviceName string, version string) map[string]string
- func SubscriptionIDs(topicIDs []string) map[string]string
- type PubSubMessage
- type PubSubPayload
Constants ¶
const ( // GoogleProjectNumberEnvVarName is a numeric project number that GoogleProjectNumberEnvVarName = "GOOGLE_PROJECT_NUMBER" // GoogleCloudProjectIDEnvVarName is used to determine the ID of the GCP project e.g for setting up StackDriver client GoogleCloudProjectIDEnvVarName = "GOOGLE_CLOUD_PROJECT" )
#nosec
const ( PubSubHandlerPath = "/pubsub" // TODO: make this Env Vars Aud = "bewell.co.ke" )
pubsub constants
Variables ¶
var (
// TimeLocation default timezone
TimeLocation, _ = time.LoadLocation("Africa/Nairobi")
)
Functions ¶
func EnsureSubscriptionsExist ¶
func EnsureSubscriptionsExist( ctx context.Context, pubsubClient *pubsub.Client, topicSubscriptionMap map[string]string, callbackURL string, ) error
EnsureSubscriptionsExist ensures that the subscriptions named in the supplied topic:subscription map exist. If any does not exist, it is created.
func EnsureTopicsExist ¶
func EnsureTopicsExist( ctx context.Context, pubsubClient *pubsub.Client, topicIDs []string, ) error
EnsureTopicsExist creates the topic(s) in the suppplied list if they do not already exist.
func GetPubSubTopic ¶
func GetPubSubTopic(m *PubSubPayload) (string, error)
GetPubSubTopic retrieves a pubsub topic from a pubsub payload.
It follows a convention where the topic is sent as an attribute under the `topicID` key.
func GetPushSubscriptionConfig ¶
func GetPushSubscriptionConfig( ctx context.Context, pubsubClient *pubsub.Client, topicID string, callbackURL string, ) (*pubsub.SubscriptionConfig, error)
GetPushSubscriptionConfig creates a push subscription configuration with the supplied parameters.
func GetServiceAccountEmail ¶
GetServiceAccountEmail inspects the environment to get the project number and uses that to compose an email to use as a Google Cloud pub-sub email
func NamespacePubsubIdentifier ¶
func NamespacePubsubIdentifier( serviceName string, topicID string, environment string, version string, ) string
NamespacePubsubIdentifier uses the service name, environment and version to create a "namespaced" pubsub identifier. This could be a topicID or subscriptionID.
func PublishToPubsub ¶
func PublishToPubsub( ctx context.Context, pubsubClient *pubsub.Client, topicID string, environment string, serviceName string, version string, payload []byte, ) error
PublishToPubsub sends the supplied payload to the indicated topic
func ReverseSubscriptionIDs ¶
func ReverseSubscriptionIDs( topicIDs []string, environment string, serviceName string, version string, ) map[string]string
ReverseSubscriptionIDs returns a (reversed) map of subscription IDs to topicIDs
func SubscriptionIDs ¶
SubscriptionIDs returns a map of topic IDs to subscription IDs
Types ¶
type PubSubMessage ¶
type PubSubMessage struct { MessageID string `json:"messageId"` Data []byte `json:"data"` Attributes map[string]string `json:"attributes"` }
PubSubMessage is a pub-sub message payload.
See https://cloud.google.com/pubsub/docs/push for more context.
The message that is POSTed looks like the example below:
{ "message": { "attributes": { "key": "value" }, "data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==", "messageId": "136969346945" }, "subscription": "projects/myproject/subscriptions/mysubscription" }
type PubSubPayload ¶
type PubSubPayload struct { Message PubSubMessage `json:"message"` Subscription string `json:"subscription"` }
PubSubPayload is the payload of a Pub/Sub event.
func VerifyPubSubJWTAndDecodePayload ¶
func VerifyPubSubJWTAndDecodePayload( w http.ResponseWriter, r *http.Request, ) (*PubSubPayload, error)
VerifyPubSubJWTAndDecodePayload confirms that there is a valid Google signed JWT and decodes the pubsub message payload into a struct.
It's use will simplify & shorten the handler funcs that process Cloud Pubsub push notifications.