pubcontrol

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 13 Imported by: 4

README

go-pubcontrol

Author: Konstantin Bokarius kon@fanout.io

A Go convenience library for publishing messages using the EPCP protocol.

License

go-pubcontrol is offered under the MIT license. See the LICENSE file.

Installation

go get github.com/fanout/go-pubcontrol

go-pubcontrol requires jwt-go 2.2.0. To ensure that the correct version of this dependency is installed use godeps:

go get github.com/tools/godep
cd $GOPATH/src/github.com/fanout/go-pubcontrol
$GOPATH/bin/godep restore

Usage

package main

import "github.com/fanout/go-pubcontrol"
import "encoding/base64"

type HttpResponseFormat struct {
    Body string
}
func (format *HttpResponseFormat) Name() string {
    return "http-response"
}
func (format *HttpResponseFormat) Export() interface{} {
    export := make(map[string]interface{})
    export["body"] = format.Body
    return export
}

func main() {
    // PubControl can be initialized with or without an endpoint configuration.
    // Each endpoint can include optional JWT authentication info.
    // Multiple endpoints can be included in a single configuration.

    // Initialize PubControl with a single endpoint:
    decodedKey, err := base64.StdEncoding.DecodeString("<realmkey>")
    if err != nil {
        panic("Failed to base64 decode the key")
    }
    pub := pubcontrol.NewPubControl([]map[string]interface{} {
            map[string]interface{} {
            "uri": "https://api.fanout.io/realm/<myrealm>",
            "iss": "<myrealm>", 
            "key": decodedKey}})

    // Add new endpoints by applying an endpoint configuration:
    pub.ApplyConfig([]map[string]interface{} {
            map[string]interface{} { "uri": "<myendpoint_uri_1>" },
            map[string]interface{} { "uri": "<myendpoint_uri_2>" }})

    // Remove all configured endpoints:
    pub.RemoveAllClients()

    // Explicitly add an endpoint as a PubControlClient instance:
    client := pubcontrol.NewPubControlClient("<myendpoint_uri>")
    // Optionally set bearer auth: client.SetAuthBearer("<token>")
    // Optionally set JWT auth: client.SetAuthJwt(<claim>, "<key>")
    // Optionally set basic auth: client.SetAuthBasic("<user>", "<password>")
    pub.AddClient(client)

    // Create an item to publish:
    format := &HttpResponseFormat{Body: "Test Go Publish!!"} 
    item := pubcontrol.NewItem([]pubcontrol.Formatter{format}, "", "")

    // Publish across all configured endpoints:
    err = pub.Publish("<channel>", item)
    if err != nil {
        panic("Publish failed with: " + err.Error())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter interface {

	// The name of the format which should return a string. Examples
	// include 'json-object' and 'http-response'
	Name() string

	// The export method which should return a format-specific hash
	// containing the required format-specific data.
	Export() interface{}
}

The Format interface is used for all publishing formats that are wrapped in the Item struct. Examples of format implementations include JsonObjectFormat and HttpStreamFormat.

type Item

type Item struct {
	// contains filtered or unexported fields
}

The Item struct is a container used to contain one or more format implementation instances where each implementation instance is of a different type of format. An Item instance may not contain multiple implementations of the same type of format. An Item instance is then serialized into a hash that is used for publishing to clients.

func NewItem

func NewItem(formats []Formatter, id, prevId string) *Item

Initialize this struct with either a single Format implementation instance or an array of Format implementation instances. Optionally specify an ID and/or previous ID to be sent as part of the message published to the client.

func (*Item) Export

func (item *Item) Export() (map[string]interface{}, error)

The export method serializes all of the formats, ID, and previous ID into a hash that is used for publishing to clients. If more than one instance of the same type of Format implementation was specified then an error will be raised.

type ItemFormatError

type ItemFormatError struct {
	// contains filtered or unexported fields
}

An error struct used to represent an error related to item formats.

func (ItemFormatError) Error

func (e ItemFormatError) Error() string

This function returns the message associated with the ItemFormatError error struct.

type PubControl

type PubControl struct {
	// contains filtered or unexported fields
}

The PubControl struct allows a consumer to manage a set of publishing endpoints and to publish to all of those endpoints via a single publish method call. A PubControl instance can be configured either using a hash or array of hashes containing configuration information or by manually adding PubControlClient instances.

func NewPubControl

func NewPubControl(config []map[string]interface{}) *PubControl

Initialize with or without a configuration. A configuration can be applied after initialization via the apply_config method.

func (*PubControl) AddClient

func (pc *PubControl) AddClient(pcc *PubControlClient)

Add the specified PubControlClient instance.

func (*PubControl) ApplyConfig

func (pc *PubControl) ApplyConfig(config []map[string]interface{})

Apply the specified configuration to this PubControl instance. The configuration object can either be a hash or an array of hashes where each hash corresponds to a single PubControlClient instance. Each hash will be parsed and a PubControlClient will be created either using just a URI or a URI and JWT authentication information.

func (*PubControl) Publish

func (pc *PubControl) Publish(channel string, item *Item) error

The publish method for publishing the specified item to the specified channel on the configured endpoints. Different endpoints are published to in parallel, with this function waiting for them to finish. Any errors (including panics) are aggregated into one error.

func (*PubControl) RemoveAllClients

func (pc *PubControl) RemoveAllClients()

Remove all of the configured PubControlClient instances.

type PubControlClient

type PubControlClient struct {
	// contains filtered or unexported fields
}

The PubControlClient struct allows consumers to publish to an endpoint of their choice. The consumer wraps a Format struct instance in an Item struct instance and passes that to the publish method. The publish method has an optional callback parameter that is called after the publishing is complete to notify the consumer of the result.

func NewPubControlClient

func NewPubControlClient(uri string) *PubControlClient

Initialize this struct with a URL representing the publishing endpoint.

func (*PubControlClient) Publish

func (pcc *PubControlClient) Publish(channel string, item *Item) error

The publish method for publishing the specified item to the specified channel on the configured endpoint.

func (*PubControlClient) SetAuthBasic

func (pcc *PubControlClient) SetAuthBasic(username, password string)

Call this method and pass a username and password to use basic authentication with the configured endpoint.

func (*PubControlClient) SetAuthBearer

func (pcc *PubControlClient) SetAuthBearer(key string)

func (*PubControlClient) SetAuthJwt

func (pcc *PubControlClient) SetAuthJwt(claim map[string]interface{},
	key []byte)

Call this method and pass a claim and key to use JWT authentication with the configured endpoint.

type PublishError

type PublishError struct {
	// contains filtered or unexported fields
}

An error struct used to represent an error encountered during publishing.

func (PublishError) Error

func (e PublishError) Error() string

This function returns the message associated with the Publish error struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL