2017-02-18 22:10:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-12-07 22:48:44 +00:00
|
|
|
"encoding/json"
|
2017-02-18 22:10:22 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/labstack/echo"
|
2017-06-05 22:05:32 +00:00
|
|
|
"github.com/labstack/echo/middleware"
|
2018-02-20 23:49:10 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-02-18 22:10:22 +00:00
|
|
|
"github.com/zfjagann/golang-ring"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2017-12-07 22:48:44 +00:00
|
|
|
"time"
|
2017-02-18 22:10:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Api struct {
|
|
|
|
Messages ring.Ring
|
|
|
|
sync.RWMutex
|
2017-12-19 22:15:03 +00:00
|
|
|
*config.BridgeConfig
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ApiMessage struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
Username string `json:"username"`
|
2017-06-18 13:44:54 +00:00
|
|
|
UserID string `json:"userid"`
|
2017-02-18 22:10:22 +00:00
|
|
|
Avatar string `json:"avatar"`
|
2017-06-07 21:54:50 +00:00
|
|
|
Gateway string `json:"gateway"`
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "api"
|
|
|
|
|
|
|
|
func init() {
|
2018-02-20 23:20:25 +00:00
|
|
|
flog = log.WithFields(log.Fields{"prefix": protocol})
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:15:03 +00:00
|
|
|
func New(cfg *config.BridgeConfig) *Api {
|
|
|
|
b := &Api{BridgeConfig: 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{}
|
2017-12-19 22:15:03 +00:00
|
|
|
b.Messages.SetCapacity(b.Config.Buffer)
|
2017-06-05 22:05:32 +00:00
|
|
|
if b.Config.Token != "" {
|
|
|
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
|
|
|
return key == b.Config.Token, nil
|
|
|
|
}))
|
|
|
|
}
|
2017-02-18 22:10:22 +00:00
|
|
|
e.GET("/api/messages", b.handleMessages)
|
2017-12-07 22:48:44 +00:00
|
|
|
e.GET("/api/stream", b.handleStream)
|
2017-02-18 22:10:22 +00:00
|
|
|
e.POST("/api/message", b.handlePostMessage)
|
|
|
|
go func() {
|
2018-02-20 23:49:10 +00:00
|
|
|
if b.Config.BindAddress == "" {
|
|
|
|
flog.Fatalf("No BindAddress configured.")
|
|
|
|
}
|
|
|
|
flog.Infof("Listening on %s", b.Config.BindAddress)
|
2017-12-19 22:15:03 +00:00
|
|
|
flog.Fatal(e.Start(b.Config.BindAddress))
|
2017-02-18 22:10:22 +00:00
|
|
|
}()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Api) Connect() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *Api) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Api) JoinChannel(channel config.ChannelInfo) error {
|
2017-02-18 22:10:22 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Api) Send(msg config.Message) (string, error) {
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2017-09-11 20:45:15 +00:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
return "", nil
|
|
|
|
}
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Messages.Enqueue(&msg)
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", nil
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Api) handlePostMessage(c echo.Context) error {
|
2018-02-20 17:36:44 +00:00
|
|
|
message := config.Message{}
|
|
|
|
if err := c.Bind(&message); err != nil {
|
2017-02-18 22:10:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-20 17:36:44 +00:00
|
|
|
// these values are fixed
|
|
|
|
message.Channel = "api"
|
|
|
|
message.Protocol = "api"
|
|
|
|
message.Account = b.Account
|
|
|
|
message.ID = ""
|
|
|
|
message.Timestamp = time.Now()
|
2017-06-07 21:54:50 +00:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", message.Username, "api")
|
2018-02-20 17:36:44 +00:00
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
2017-12-07 22:48:44 +00:00
|
|
|
|
|
|
|
func (b *Api) handleStream(c echo.Context) error {
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
|
|
|
closeNotifier := c.Response().CloseNotify()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-closeNotifier:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|