Extract bridgeMap into own package to improve testability (#601)

This commit is contained in:
Patrick Connolly 2018-12-01 06:53:00 +08:00 committed by Wim
parent faeeee2948
commit f2088a687e
5 changed files with 43 additions and 34 deletions

View File

@ -0,0 +1,35 @@
package bridgemap
import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/api"
"github.com/42wim/matterbridge/bridge/discord"
"github.com/42wim/matterbridge/bridge/gitter"
"github.com/42wim/matterbridge/bridge/irc"
"github.com/42wim/matterbridge/bridge/matrix"
"github.com/42wim/matterbridge/bridge/mattermost"
"github.com/42wim/matterbridge/bridge/rocketchat"
"github.com/42wim/matterbridge/bridge/slack"
"github.com/42wim/matterbridge/bridge/sshchat"
"github.com/42wim/matterbridge/bridge/steam"
"github.com/42wim/matterbridge/bridge/telegram"
"github.com/42wim/matterbridge/bridge/xmpp"
"github.com/42wim/matterbridge/bridge/zulip"
)
var FullMap = map[string]bridge.Factory{
"api": api.New,
"discord": bdiscord.New,
"gitter": bgitter.New,
"irc": birc.New,
"mattermost": bmattermost.New,
"matrix": bmatrix.New,
"rocketchat": brocketchat.New,
"slack-legacy": bslack.NewLegacy,
"slack": bslack.New,
"sshchat": bsshchat.New,
"steam": bsteam.New,
"telegram": btelegram.New,
"xmpp": bxmpp.New,
"zulip": bzulip.New,
}

View File

@ -13,20 +13,7 @@ import (
"time" "time"
"github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/api"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
bdiscord "github.com/42wim/matterbridge/bridge/discord"
bgitter "github.com/42wim/matterbridge/bridge/gitter"
birc "github.com/42wim/matterbridge/bridge/irc"
bmatrix "github.com/42wim/matterbridge/bridge/matrix"
bmattermost "github.com/42wim/matterbridge/bridge/mattermost"
brocketchat "github.com/42wim/matterbridge/bridge/rocketchat"
bslack "github.com/42wim/matterbridge/bridge/slack"
bsshchat "github.com/42wim/matterbridge/bridge/sshchat"
bsteam "github.com/42wim/matterbridge/bridge/steam"
btelegram "github.com/42wim/matterbridge/bridge/telegram"
bxmpp "github.com/42wim/matterbridge/bridge/xmpp"
bzulip "github.com/42wim/matterbridge/bridge/zulip"
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib" "github.com/peterhellberg/emojilib"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -53,23 +40,6 @@ type BrMsgID struct {
var flog *log.Entry var flog *log.Entry
var bridgeMap = map[string]bridge.Factory{
"api": api.New,
"discord": bdiscord.New,
"gitter": bgitter.New,
"irc": birc.New,
"mattermost": bmattermost.New,
"matrix": bmatrix.New,
"rocketchat": brocketchat.New,
"slack-legacy": bslack.NewLegacy,
"slack": bslack.New,
"sshchat": bsshchat.New,
"steam": bsteam.New,
"telegram": btelegram.New,
"xmpp": bxmpp.New,
"zulip": bzulip.New,
}
const ( const (
apiProtocol = "api" apiProtocol = "api"
) )
@ -114,7 +84,7 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
br.Log = log.WithFields(log.Fields{"prefix": "bridge"}) br.Log = log.WithFields(log.Fields{"prefix": "bridge"})
brconfig := &bridge.Config{Remote: gw.Message, Log: log.WithFields(log.Fields{"prefix": br.Protocol}), Bridge: br} brconfig := &bridge.Config{Remote: gw.Message, Log: log.WithFields(log.Fields{"prefix": br.Protocol}), Bridge: br}
// add the actual bridger for this protocol to this bridge using the bridgeMap // add the actual bridger for this protocol to this bridge using the bridgeMap
br.Bridger = bridgeMap[br.Protocol](brconfig) br.Bridger = gw.Router.BridgeMap[br.Protocol](brconfig)
} }
gw.mapChannelsToBridge(br) gw.mapChannelsToBridge(br)
gw.Bridges[cfg.Account] = br gw.Bridges[cfg.Account] = br

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway/bridgemap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
@ -160,7 +161,7 @@ const (
func maketestRouter(input []byte) *Router { func maketestRouter(input []byte) *Router {
cfg := config.NewConfigFromString(input) cfg := config.NewConfigFromString(input)
r, err := NewRouter(cfg) r, err := NewRouter(cfg, bridgemap.FullMap)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

View File

@ -12,14 +12,16 @@ import (
type Router struct { type Router struct {
config.Config config.Config
BridgeMap map[string]bridge.Factory
Gateways map[string]*Gateway Gateways map[string]*Gateway
Message chan config.Message Message chan config.Message
MattermostPlugin chan config.Message MattermostPlugin chan config.Message
} }
func NewRouter(cfg config.Config) (*Router, error) { func NewRouter(cfg config.Config, bridgeMap map[string]bridge.Factory) (*Router, error) {
r := &Router{ r := &Router{
Config: cfg, Config: cfg,
BridgeMap: bridgeMap,
Message: make(chan config.Message), Message: make(chan config.Message),
MattermostPlugin: make(chan config.Message), MattermostPlugin: make(chan config.Message),
Gateways: make(map[string]*Gateway), Gateways: make(map[string]*Gateway),

View File

@ -8,6 +8,7 @@ import (
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway" "github.com/42wim/matterbridge/gateway"
"github.com/42wim/matterbridge/gateway/bridgemap"
"github.com/google/gops/agent" "github.com/google/gops/agent"
prefixed "github.com/matterbridge/logrus-prefixed-formatter" prefixed "github.com/matterbridge/logrus-prefixed-formatter"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -45,7 +46,7 @@ func main() {
} }
cfg := config.NewConfig(*flagConfig) cfg := config.NewConfig(*flagConfig)
cfg.BridgeValues().General.Debug = *flagDebug cfg.BridgeValues().General.Debug = *flagDebug
r, err := gateway.NewRouter(cfg) r, err := gateway.NewRouter(cfg, bridgemap.FullMap)
if err != nil { if err != nil {
flog.Fatalf("Starting gateway failed: %s", err) flog.Fatalf("Starting gateway failed: %s", err)
} }