This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanout.go
53 lines (46 loc) · 1.75 KB
/
fanout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// fanout.go
// ~~~~~~~~~
// This module implements the Fanout struct and features.
// :authors: Konstantin Bokarius.
// :copyright: (c) 2015 by Fanout, Inc.
// :license: MIT, see LICENSE for more details.
package fanout
import "github.com/fanout/go-pubcontrol"
// The Fanout struct is used for publishing messages to Fanout.io and is
// configured with a Fanout.io realm and associated key. SSL can either
// be enabled or disabled.
type Fanout struct {
pubControl *pubcontrol.PubControl
}
// Initialize with a specified realm, key, and a boolean indicating wther
// SSL should be enabled or disabled.
func NewFanout(realm string, key []byte, ssl bool) *Fanout {
fanout := new(Fanout)
fanout.pubControl = pubcontrol.NewPubControl([]map[string]interface{} {
map[string]interface{} {
"uri": formatUri(realm, ssl),
"iss": realm,
"key": key}})
return fanout
}
// An internal method used for formatting the Fanout.io URI.
func formatUri(realm string, ssl bool) string {
protocol := "https://"
if (!ssl) {
protocol = "http://"
}
return protocol + "api.fanout.io/realm/" + realm
}
// Publish the specified data to the specified channel for the configured
// Fanout.io realm. Optionally provide an ID and previous ID to send along
// with the message.
func (fanout *Fanout) Publish(channel string, data, id, prev_id string) error {
return fanout.pubControl.Publish(channel, getItem(data, id, prev_id))
}
// An internal method used for getting an item based on the specified
// parameters.
func getItem(data, id, prev_id string) *pubcontrol.Item {
format := &JsonObjectFormat{value: data}
return pubcontrol.NewItem([]pubcontrol.Formatter{format},
id, prev_id)
}