From aeb06c747812e8aa554495bf3eaaa099da555906 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Mon, 15 Oct 2018 03:50:44 +0800 Subject: [PATCH] Moved translation client from bridge to router. --- bridge/config/config.go | 2 ++ gateway/gateway.go | 20 ++------------------ gateway/router.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index ca75e692..fac68977 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -11,6 +11,7 @@ import ( prefixed "github.com/matterbridge/logrus-prefixed-formatter" log "github.com/sirupsen/logrus" "github.com/spf13/viper" + "cloud.google.com/go/translate" ) const ( @@ -169,6 +170,7 @@ type ConfigValues struct { Zulip map[string]Protocol General Protocol Gateway []Gateway + GTClient *translate.Client SameChannelGateway []SameChannelGateway } diff --git a/gateway/gateway.go b/gateway/gateway.go index cad28d30..fbe65f07 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -2,7 +2,6 @@ package gateway import ( "bytes" - b64 "encoding/base64" "fmt" "io/ioutil" "net/http" @@ -34,8 +33,6 @@ import ( log "github.com/sirupsen/logrus" // "github.com/davecgh/go-spew/spew" "cloud.google.com/go/translate" - "google.golang.org/api/option" - "golang.org/x/oauth2/google" "golang.org/x/text/language" "crypto/sha1" "path/filepath" @@ -56,7 +53,6 @@ type Gateway struct { Message chan config.Message Name string Messages *lru.Cache - GTClient *translate.Client } type BrMsgID struct { @@ -93,18 +89,6 @@ func New(cfg config.Gateway, r *Router) *Gateway { cache, _ := lru.New(5000) gw.Messages = cache gw.AddConfig(&cfg) - - var err error - ctx := context.Background() - credsJson, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_BASE64")) - creds, _ := google.CredentialsFromJSON(ctx, credsJson, translate.Scope) - gw.GTClient, err = translate.NewClient(ctx, option.WithCredentials(creds)) - if err != nil { - flog.Warnf("Google Translate API failed to authorize: " + err.Error()) - } else { - flog.Infof("Google Translation enabled.") - } - return gw } @@ -346,7 +330,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Username = gw.modifyUsername(origmsg, dest) msg.ID = "" - if (gw.GTClient != nil) && (channel.Options.Locale != "") { + if (gw.Router.GTClient != nil) && (channel.Options.Locale != "") { attribution, ok := os.LookupEnv("GOOGLE_TRANSLATE_ATTRIBUTION") if !(ok) { @@ -356,7 +340,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM ctx := context.Background() lang, _ := language.Parse(channel.Options.Locale) - client := gw.GTClient + client := gw.Router.GTClient defer client.Close() text := msg.Text diff --git a/gateway/router.go b/gateway/router.go index 3a45de36..5b3d3de8 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -1,12 +1,18 @@ package gateway import ( + "context" + b64 "encoding/base64" "fmt" + "os" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" samechannelgateway "github.com/42wim/matterbridge/gateway/samechannel" // "github.com/davecgh/go-spew/spew" + "cloud.google.com/go/translate" + "google.golang.org/api/option" + "golang.org/x/oauth2/google" "time" ) @@ -21,6 +27,17 @@ func NewRouter(cfg *config.Config) (*Router, error) { sgw := samechannelgateway.New(cfg) gwconfigs := sgw.GetConfig() + var err error + ctx := context.Background() + credsJson, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_BASE64")) + creds, _ := google.CredentialsFromJSON(ctx, credsJson, translate.Scope) + r.GTClient, err = translate.NewClient(ctx, option.WithCredentials(creds)) + if err != nil { + flog.Warnf("Google Translate API failed to authorize: " + err.Error()) + } else { + flog.Infof("Google Translation enabled.") + } + for _, entry := range append(gwconfigs, cfg.Gateway...) { if !entry.Enable { continue