summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-02-18 23:10:22 +0100
committerWim <wim@42.be>2017-02-18 23:13:46 +0100
commit73f01ad8d813505c6309fe568d0abc597cff8790 (patch)
tree426bc1779c1abcf2afe56e2dc2a2f43d70aae278 /bridge
parent930b639cc9cd2d2873302f30303378c0e53816a8 (diff)
downloadmatterbridge-msglm-73f01ad8d813505c6309fe568d0abc597cff8790.tar.gz
matterbridge-msglm-73f01ad8d813505c6309fe568d0abc597cff8790.tar.bz2
matterbridge-msglm-73f01ad8d813505c6309fe568d0abc597cff8790.zip
Add REST API support
Diffstat (limited to 'bridge')
-rw-r--r--bridge/api/api.go91
-rw-r--r--bridge/bridge.go4
-rw-r--r--bridge/config/config.go17
3 files changed, 106 insertions, 6 deletions
diff --git a/bridge/api/api.go b/bridge/api/api.go
new file mode 100644
index 00000000..4870ee6a
--- /dev/null
+++ b/bridge/api/api.go
@@ -0,0 +1,91 @@
+package api
+
+import (
+ "github.com/42wim/matterbridge/bridge/config"
+ log "github.com/Sirupsen/logrus"
+ "github.com/labstack/echo"
+ "github.com/zfjagann/golang-ring"
+ "net/http"
+ "sync"
+)
+
+type Api struct {
+ Config *config.Protocol
+ Remote chan config.Message
+ Account string
+ Messages ring.Ring
+ sync.RWMutex
+}
+
+type ApiMessage struct {
+ Text string `json:"text"`
+ Username string `json:"username"`
+ Avatar string `json:"avatar"`
+}
+
+var flog *log.Entry
+var protocol = "api"
+
+func init() {
+ flog = log.WithFields(log.Fields{"module": protocol})
+}
+
+func New(cfg config.Protocol, account string, c chan config.Message) *Api {
+ b := &Api{}
+ e := echo.New()
+ b.Messages = ring.Ring{}
+ b.Messages.SetCapacity(cfg.Buffer)
+ b.Config = &cfg
+ b.Account = account
+ b.Remote = c
+ e.GET("/api/messages", b.handleMessages)
+ e.POST("/api/message", b.handlePostMessage)
+ go func() {
+ flog.Fatal(e.Start(cfg.BindAddress))
+ }()
+ return b
+}
+
+func (b *Api) Connect() error {
+ return nil
+}
+func (b *Api) Disconnect() error {
+ return nil
+
+}
+func (b *Api) JoinChannel(channel string) error {
+ return nil
+
+}
+
+func (b *Api) Send(msg config.Message) error {
+ b.Lock()
+ defer b.Unlock()
+ b.Messages.Enqueue(&msg)
+ return nil
+}
+
+func (b *Api) handlePostMessage(c echo.Context) error {
+ message := &ApiMessage{}
+ if err := c.Bind(message); err != nil {
+ return err
+ }
+ b.Remote <- config.Message{
+ Text: message.Text,
+ Username: message.Username,
+ Channel: "api",
+ Avatar: message.Avatar,
+ Account: b.Account,
+ }
+ return c.JSON(http.StatusOK, message)
+}
+
+func (b *Api) handleMessages(c echo.Context) error {
+ b.Lock()
+ defer b.Unlock()
+ for _, msg := range b.Messages.Values() {
+ c.JSONPretty(http.StatusOK, msg, " ")
+ }
+ b.Messages = ring.Ring{}
+ return nil
+}
diff --git a/bridge/bridge.go b/bridge/bridge.go
index db26c422..012b0efd 100644
--- a/bridge/bridge.go
+++ b/bridge/bridge.go
@@ -1,6 +1,7 @@
package bridge
import (
+ "github.com/42wim/matterbridge/bridge/api"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/discord"
"github.com/42wim/matterbridge/bridge/gitter"
@@ -70,6 +71,9 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid
case "rocketchat":
b.Config = cfg.Rocketchat[name]
b.Bridger = brocketchat.New(cfg.Rocketchat[name], bridge.Account, c)
+ case "api":
+ b.Config = cfg.Api[name]
+ b.Bridger = api.New(cfg.Api[name], bridge.Account, c)
}
return b
}
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 811c97ae..4f6568ab 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -6,6 +6,7 @@ import (
"os"
"reflect"
"strings"
+ "time"
)
const (
@@ -14,16 +15,19 @@ const (
)
type Message struct {
- Text string
- Channel string
- Username string
- Avatar string
- Account string
- Event string
+ Text string
+ Channel string
+ Username string
+ Avatar string
+ Account string
+ Event string
+ Protocol string
+ Timestamp time.Time
}
type Protocol struct {
BindAddress string // mattermost, slack
+ Buffer int // api
IconURL string // mattermost, slack
IgnoreNicks string // all protocols
Jid string // xmpp
@@ -79,6 +83,7 @@ type SameChannelGateway struct {
}
type Config struct {
+ Api map[string]Protocol
IRC map[string]Protocol
Mattermost map[string]Protocol
Slack map[string]Protocol