Skip to content

Commit

Permalink
Added ability to load remote configuration files. [42wim#525]
Browse files Browse the repository at this point in the history
  • Loading branch information
patcon committed Nov 7, 2018
1 parent a20b789 commit 1a18493
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Minecraft server chat support via [MatterLink](https://github.com/elytra/MatterL
* [Support multiple gateways(bridges) for your protocols](https://github.com/42wim/matterbridge/wiki/Features#support-multiple-gatewaysbridges-for-your-protocols)
* [Message edits and deletes](https://github.com/42wim/matterbridge/wiki/Features#message-edits-and-deletes)
* Preserves threading when possible
* Remote configuration files
* [Attachment / files handling](https://github.com/42wim/matterbridge/wiki/Features#attachment--files-handling)
* [Username and avatar spoofing](https://github.com/42wim/matterbridge/wiki/Features#username-and-avatar-spoofing)
* [Private groups](https://github.com/42wim/matterbridge/wiki/Features#private-groups)
Expand Down
38 changes: 38 additions & 0 deletions bridge/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package api

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -41,7 +43,9 @@ func New(cfg *bridge.Config) bridge.Bridger {
return key == b.GetString("Token"), nil
}))
}

e.GET("/api/health", b.handleHealthcheck)
e.PUT("/api/reload", b.handleConfigReload)
e.GET("/api/messages", b.handleMessages)
e.GET("/api/stream", b.handleStream)
e.POST("/api/message", b.handlePostMessage)
Expand Down Expand Up @@ -82,6 +86,40 @@ func (b *Api) handleHealthcheck(c echo.Context) error {
return c.String(http.StatusOK, "OK")
}

func (b *Api) handleConfigReload(c echo.Context) error {
cfgURL := b.GetString("ConfigURL")
if cfgURL == "" {
b.Log.Warning("Reload API triggered, but no config file url set.")
return c.String(http.StatusInternalServerError, "Internal Server Error")
}

b.Log.Debugf("Reloading config from remote file: " + cfgURL)
_, err := url.ParseRequestURI(cfgURL)
if err != nil {
b.Log.Error("Malformed config file url: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
res, err := http.Get(cfgURL)
defer res.Body.Close()
if err != nil {
b.Log.Error("Failed to fetch remote config file: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
b.Log.Error("Error reading remote config file: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
cfgfile := b.GetConfigFile()
err = ioutil.WriteFile(cfgfile, content, 0644)
if err != nil {
b.Log.Error("Failed to write remote config file: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}

return c.String(http.StatusAccepted, "Accepted")
}

func (b *Api) handlePostMessage(c echo.Context) error {
message := config.Message{}
if err := c.Bind(&message); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map
return nil
}

func (b *Bridge) GetConfigFile() string {
return b.Config.GetConfigFile()
}

func (b *Bridge) GetBool(key string) bool {
if b.Config.GetBool(b.Account + "." + key) {
return b.Config.GetBool(b.Account + "." + key)
Expand Down
7 changes: 7 additions & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Protocol struct {
Buffer int // api
Charset string // irc
ColorNicks bool // only irc for now
ConfigURL string // api
Debug bool // general
DebugLevel int // only for irc now
EditSuffix string // mattermost, slack, discord, telegram, gitter
Expand Down Expand Up @@ -172,6 +173,7 @@ type ConfigValues struct {
General Protocol
Gateway []Gateway
SameChannelGateway []SameChannelGateway
ConfigFile string
}

type Config struct {
Expand Down Expand Up @@ -212,6 +214,7 @@ func NewConfig(cfgfile string) *Config {
flog.Println("Config file changed:", e.Name)
})

mycfg.v.Set("ConfigFile", cfgfile)
mycfg.ConfigValues = &cfg
return mycfg
}
Expand All @@ -233,6 +236,10 @@ func NewConfigFromString(input []byte) *Config {
return mycfg
}

func (c *Config) GetConfigFile() string {
return c.v.GetString("ConfigFile")
}

func (c *Config) GetBool(key string) bool {
c.RLock()
defer c.RUnlock()
Expand Down

0 comments on commit 1a18493

Please sign in to comment.