Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow simple remote configuration files #565

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method Api.handleConfigReload has 6 return statements (exceeds 4 allowed).

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)
if err != nil {
b.Log.Error("Failed to fetch remote config file: ", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
defer res.Body.Close()
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.Config.Bridge.Config.ConfigFileUsed()
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
5 changes: 5 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 @@ -229,6 +230,10 @@ func NewConfigFromString(input []byte) *Config {
return mycfg
}

func (c *Config) ConfigFileUsed() string {
return c.v.ConfigFileUsed()
}

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