Skip to content

Commit

Permalink
PoC for getting router to reload gateway config.
Browse files Browse the repository at this point in the history
  • Loading branch information
patcon committed Nov 7, 2018
1 parent 775af76 commit a0799e7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
21 changes: 15 additions & 6 deletions bridge/api/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
Expand All @@ -13,9 +14,11 @@ import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/zfjagann/golang-ring"
"github.com/spf13/viper"
)

type Api struct {
Server *echo.Echo
Messages ring.Ring
sync.RWMutex
*bridge.Config
Expand All @@ -30,8 +33,8 @@ type ApiMessage struct {
}

func New(cfg *bridge.Config) bridge.Bridger {
b := &Api{Config: cfg}
e := echo.New()
b := &Api{Config: cfg, Server: e}
e.HideBanner = true
e.HidePort = true
b.Messages = ring.Ring{}
Expand All @@ -49,20 +52,24 @@ func New(cfg *bridge.Config) bridge.Bridger {
e.GET("/api/messages", b.handleMessages)
e.GET("/api/stream", b.handleStream)
e.POST("/api/message", b.handlePostMessage)
return b
}

func (b *Api) Connect() error {
go func() {
if b.GetString("BindAddress") == "" {
b.Log.Fatalf("No BindAddress configured.")
}
b.Log.Infof("Listening on %s", b.GetString("BindAddress"))
b.Log.Fatal(e.Start(b.GetString("BindAddress")))
b.Log.Info(b.Server.Start(b.GetString("BindAddress")))
}()
return b
}

func (b *Api) Connect() error {
return nil
}
func (b *Api) Disconnect() error {
ctx := context.Background()
if err := b.Server.Shutdown(ctx); err != nil {
b.Log.Info(err)
}
return nil

}
Expand Down Expand Up @@ -116,7 +123,9 @@ func (b *Api) handleConfigReload(c echo.Context) error {
b.Log.Error("Failed to write remote config file: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
viper.ReadInConfig()

b.Remote <- config.Message{Username: "system", Text: config.EVENT_RELOAD_CONFIG, Channel: "api", Account: "", Event: config.EVENT_RELOAD_CONFIG}
return c.String(http.StatusAccepted, "Accepted")
}

Expand Down
2 changes: 2 additions & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
EVENT_USER_ACTION = "user_action"
EVENT_MSG_DELETE = "msg_delete"
EVENT_API_CONNECTED = "api_connected"
EVENT_RELOAD_CONFIG = "reload_config"
)

type Message struct {
Expand Down Expand Up @@ -211,6 +212,7 @@ func NewConfig(cfgfile string) *Config {
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
viper.Unmarshal(&cfg)
flog.Println("Config file changed:", e.Name)
})

Expand Down
32 changes: 32 additions & 0 deletions gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ func (r *Router) Start() error {
return nil
}

func (r *Router) Stop() error {
for _, gw := range r.Gateways {
for _, br := range gw.Bridges {
err := br.Disconnect()
if err != nil {
return err
}
}
}
return nil
}

func (r *Router) Restart() error {
if err := r.Stop(); err != nil {
return err
}

r, err = NewRouter(r.Config)
if err != nil {
return err
}

if err = r.Start(); err != nil {
return err
}

return nil
}

func (r *Router) getBridge(account string) *bridge.Bridge {
for _, gw := range r.Gateways {
if br, ok := gw.Bridges[account]; ok {
Expand All @@ -70,6 +99,9 @@ func (r *Router) getBridge(account string) *bridge.Bridge {

func (r *Router) handleReceive() {
for msg := range r.Message {
if msg.Event == config.EVENT_RELOAD_CONFIG {
go r.Restart()
}
if msg.Event == config.EVENT_FAILURE {
Loop:
for _, gw := range r.Gateways {
Expand Down
7 changes: 7 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,13 @@ Buffer=1000
#OPTIONAL (no authorization if token is empty)
Token="mytoken"

#URL from which to fetch and reload configuration with API call:
# PUT /api/reload
#Note: Affects global configuration, not just bridge or gateway!
#Highly recommended to use token auth if using this.
#OPTIONAL (default empty)
ConfigURL="https://example.com/matterbridge.toml"

#extra label that can be used in the RemoteNickFormat
#optional (default empty)
Label=""
Expand Down

0 comments on commit a0799e7

Please sign in to comment.