2
0
mirror of https://github.com/42wim/matterbridge synced 2024-11-03 15:40:24 +00:00
matterbridge/bridge/api/api.go

131 lines
2.9 KiB
Go
Raw Normal View History

2017-02-18 22:10:22 +00:00
package api
import (
"encoding/json"
"net/http"
"sync"
"time"
2018-02-26 23:33:21 +00:00
"github.com/42wim/matterbridge/bridge"
2017-02-18 22:10:22 +00:00
"github.com/42wim/matterbridge/bridge/config"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2017-02-18 22:10:22 +00:00
"github.com/zfjagann/golang-ring"
)
type API struct {
2017-02-18 22:10:22 +00:00
Messages ring.Ring
sync.RWMutex
*bridge.Config
2017-02-18 22:10:22 +00:00
}
type Message struct {
2017-02-18 22:10:22 +00:00
Text string `json:"text"`
Username string `json:"username"`
UserID string `json:"userid"`
2017-02-18 22:10:22 +00:00
Avatar string `json:"avatar"`
Gateway string `json:"gateway"`
2017-02-18 22:10:22 +00:00
}
func New(cfg *bridge.Config) bridge.Bridger {
b := &API{Config: cfg}
2017-02-18 22:10:22 +00:00
e := echo.New()
2018-02-20 23:49:10 +00:00
e.HideBanner = true
e.HidePort = true
2017-02-18 22:10:22 +00:00
b.Messages = ring.Ring{}
if b.GetInt("Buffer") != 0 {
b.Messages.SetCapacity(b.GetInt("Buffer"))
}
if b.GetString("Token") != "" {
2017-06-05 22:05:32 +00:00
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return key == b.GetString("Token"), nil
2017-06-05 22:05:32 +00:00
}))
}
2018-11-07 08:11:59 +00:00
e.GET("/api/health", b.handleHealthcheck)
2017-02-18 22:10:22 +00:00
e.GET("/api/messages", b.handleMessages)
e.GET("/api/stream", b.handleStream)
2017-02-18 22:10:22 +00:00
e.POST("/api/message", b.handlePostMessage)
go func() {
if b.GetString("BindAddress") == "" {
2018-02-26 23:33:21 +00:00
b.Log.Fatalf("No BindAddress configured.")
2018-02-20 23:49:10 +00:00
}
b.Log.Infof("Listening on %s", b.GetString("BindAddress"))
b.Log.Fatal(e.Start(b.GetString("BindAddress")))
2017-02-18 22:10:22 +00:00
}()
return b
}
func (b *API) Connect() error {
2017-02-18 22:10:22 +00:00
return nil
}
func (b *API) Disconnect() error {
2017-02-18 22:10:22 +00:00
return nil
}
func (b *API) JoinChannel(channel config.ChannelInfo) error {
2017-02-18 22:10:22 +00:00
return nil
}
func (b *API) Send(msg config.Message) (string, error) {
2017-02-18 22:10:22 +00:00
b.Lock()
defer b.Unlock()
// ignore delete messages
if msg.Event == config.EventMsgDelete {
return "", nil
}
2017-02-18 22:10:22 +00:00
b.Messages.Enqueue(&msg)
return "", nil
2017-02-18 22:10:22 +00:00
}
func (b *API) handleHealthcheck(c echo.Context) error {
2018-11-07 08:11:59 +00:00
return c.String(http.StatusOK, "OK")
}
func (b *API) handlePostMessage(c echo.Context) error {
message := config.Message{}
if err := c.Bind(&message); err != nil {
2017-02-18 22:10:22 +00:00
return err
}
// these values are fixed
message.Channel = "api"
message.Protocol = "api"
message.Account = b.Account
message.ID = ""
message.Timestamp = time.Now()
2018-02-26 23:33:21 +00:00
b.Log.Debugf("Sending message from %s on %s to gateway", message.Username, "api")
b.Remote <- message
2017-02-18 22:10:22 +00:00
return c.JSON(http.StatusOK, message)
}
func (b *API) handleMessages(c echo.Context) error {
2017-02-18 22:10:22 +00:00
b.Lock()
defer b.Unlock()
2017-06-05 21:08:36 +00:00
c.JSONPretty(http.StatusOK, b.Messages.Values(), " ")
2017-02-18 22:10:22 +00:00
b.Messages = ring.Ring{}
return nil
}
func (b *API) handleStream(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().WriteHeader(http.StatusOK)
greet := config.Message{
Event: config.EventAPIConnected,
Timestamp: time.Now(),
}
if err := json.NewEncoder(c.Response()).Encode(greet); err != nil {
return err
}
c.Response().Flush()
for {
msg := b.Messages.Dequeue()
if msg != nil {
if err := json.NewEncoder(c.Response()).Encode(msg); err != nil {
return err
}
c.Response().Flush()
}
time.Sleep(200 * time.Millisecond)
}
}