Categorygithub.com/fanout/go-pubcontrol
modulepackage
1.2.0
Repository: https://github.com/fanout/go-pubcontrol.git
Documentation: pkg.go.dev

# README

go-pubcontrol

Author: Konstantin Bokarius [email protected]

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())
    }
}

# Functions

Initialize this struct with either a single Format implementation instance or an array of Format implementation instances.
Initialize with or without a configuration.
Initialize this struct with a URL representing the publishing endpoint.

# Structs

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 error struct used to represent an error related to item formats.
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.
The PubControlClient struct allows consumers to publish to an endpoint of their choice.
An error struct used to represent an error encountered during publishing.

# Interfaces

The Format interface is used for all publishing formats that are wrapped in the Item struct.