From bff1050eabc5aaa40840efb234a0b74d48d2766c Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Wed, 29 Jun 2022 13:16:43 +0500 Subject: [PATCH] Make linters happy. --- .golangci.yml | 2 ++ config/config.go | 29 ++++++++++--------- config/exported.go | 6 ++-- context/context.go | 20 ++++++------- context/exported.go | 2 +- pushers/matrix/exported.go | 6 ++-- pushers/matrix/matrixconnection.go | 40 +++++++++++++------------- pushers/matrix/matrixpusher.go | 14 ++++----- pushers/telegram/exported.go | 6 ++-- pushers/telegram/telegramconnection.go | 18 ++++++------ pushers/telegram/telegrampusher.go | 14 ++++----- slack/exported.go | 6 ++-- slack/slackapiserver.go | 12 ++++---- slack/slackhandler.go | 22 +++++++------- 14 files changed, 100 insertions(+), 97 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8364ca4..9ba5341 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,8 @@ linters: - gochecknoglobals # This linter goes crazy for nothing (almost). - funlen + # Deprecated things. + - exhaustivestruct linters-settings: lll: line-length: 128 diff --git a/config/config.go b/config/config.go index 3c7a9e8..40afe46 100644 --- a/config/config.go +++ b/config/config.go @@ -50,7 +50,7 @@ func (conf Configuration) GetTempValue(key string) (string, error) { if value[0] == '~' { usr, err := user.Current() if err != nil { - c.Log.Fatal().Err(err).Msg("Failed to get current user data") + ctx.Log.Fatal().Err(err).Msg("Failed to get current user data") } value = strings.Replace(value, "~", usr.HomeDir, 1) @@ -60,7 +60,7 @@ func (conf Configuration) GetTempValue(key string) (string, error) { } func (conf Configuration) Initialize() { - c.Log.Info().Msg("Initializing configuration storage...") + ctx.Log.Info().Msg("Initializing configuration storage...") tempconfig = make(map[string]string) @@ -71,26 +71,26 @@ func (conf Configuration) Initialize() { DefaultValue: "~/.config/OpenSAPS/config.yaml", } - _ = c.Flagger.AddFlag(&flagConfigpath) + _ = ctx.Flagger.AddFlag(&flagConfigpath) } // Initializes configuration root path for later usage. func (conf Configuration) initializeConfigurationFilePath() { - c.Log.Debug().Msg("Asking flagger about configuration root path supplied by user...") + ctx.Log.Debug().Msg("Asking flagger about configuration root path supplied by user...") - configpath, err := c.Flagger.GetStringValue("config") + configpath, err := ctx.Flagger.GetStringValue("config") if err != nil { - c.Log.Fatal().Msg("Something went wrong - Flagger doesn't know about \"-config\" parameter!") + ctx.Log.Fatal().Msg("Something went wrong - Flagger doesn't know about \"-config\" parameter!") } - c.Log.Info().Msg("Will use configuration file: '" + configpath + "'") + ctx.Log.Info().Msg("Will use configuration file: '" + configpath + "'") conf.SetTempValue("CONFIGURATION_FILE", configpath) } // Asking Flagger about flags, initialize internal variables. // Should be called **after** Flagger.Parse(). func (conf Configuration) InitializeLater() { - c.Log.Info().Msg("Completing configuration initialization...") + ctx.Log.Info().Msg("Completing configuration initialization...") conf.initializeConfigurationFilePath() } @@ -99,26 +99,27 @@ func (conf Configuration) InitializeLater() { func (conf Configuration) LoadConfigurationFromFile() { configpath, err := conf.GetTempValue("CONFIGURATION_FILE") if err != nil { - c.Log.Fatal().Msg("Failed to get configuration file path from internal temporary configuration storage! OpenSAPS is BROKEN!") + ctx.Log.Fatal(). + Msg("Failed to get configuration file path from internal temporary configuration storage! OpenSAPS is BROKEN!") } - c.Log.Info().Msgf("Loading configuration from '%s'...", configpath) + ctx.Log.Info().Msgf("Loading configuration from '%s'...", configpath) // Read file into memory. configBytes, err1 := ioutil.ReadFile(configpath) if err1 != nil { - c.Log.Fatal().Msgf("Error occurred while reading configuration file: %s", err1.Error()) + ctx.Log.Fatal().Msgf("Error occurred while reading configuration file: %s", err1.Error()) } - // nolint:exhaustivestruct + // nolint:exhaustruct config = &configstruct.ConfigStruct{} // Parse YAML. err2 := yaml.Unmarshal(configBytes, config) if err2 != nil { - c.Log.Fatal().Msgf("Failed to parse configuration file: %s", err2.Error()) + ctx.Log.Fatal().Msgf("Failed to parse configuration file: %s", err2.Error()) } - c.Log.Debug().Msgf("Loaded configuration: %+v", config) + ctx.Log.Debug().Msgf("Loaded configuration: %+v", config) } // Sets value to key in temporary configuration storage. diff --git a/config/exported.go b/config/exported.go index 0a51d7e..d59f92c 100644 --- a/config/exported.go +++ b/config/exported.go @@ -24,7 +24,7 @@ import ( ) var ( - c *context.Context + ctx *context.Context // Temporary configuration. tempconfig map[string]string // Configuration from YAML file. @@ -32,7 +32,7 @@ var ( ) func New(cc *context.Context) { - c = cc + ctx = cc conf := Configuration{} - c.RegisterConfigurationInterface(configurationinterface.ConfigurationInterface(conf)) + ctx.RegisterConfigurationInterface(configurationinterface.ConfigurationInterface(conf)) } diff --git a/context/context.go b/context/context.go index b3e0f05..18781eb 100644 --- a/context/context.go +++ b/context/context.go @@ -45,32 +45,32 @@ func (c *Context) Initialize() { c.Parsers = make(map[string]parserinterface.ParserInterface) c.Pushers = make(map[string]pusherinterface.PusherInterface) - // nolint:exhaustivestruct + // nolint:exhaustruct output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339} output.FormatLevel = func(lvlRaw interface{}) string { - var v string + var formattedLvl string if lvl, ok := lvlRaw.(string); ok { lvl = strings.ToUpper(lvl) switch lvl { case "DEBUG": - v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl) case "ERROR": - v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl) case "FATAL": - v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl) case "INFO": - v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl) case "PANIC": - v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl) case "WARN": - v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl) + formattedLvl = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl) default: - v = lvl + formattedLvl = lvl } } - return fmt.Sprintf("| %s |", v) + return fmt.Sprintf("| %s |", formattedLvl) } c.Log = zerolog.New(output).With().Timestamp().Logger() diff --git a/context/exported.go b/context/exported.go index 2e6d9ef..413908c 100644 --- a/context/exported.go +++ b/context/exported.go @@ -18,6 +18,6 @@ package context func New() *Context { - // nolint:exhaustivestruct + // nolint:exhaustruct return &Context{} } diff --git a/pushers/matrix/exported.go b/pushers/matrix/exported.go index 6fc45b1..e640284 100644 --- a/pushers/matrix/exported.go +++ b/pushers/matrix/exported.go @@ -23,14 +23,14 @@ import ( ) var ( - c *context.Context + ctx *context.Context connections map[string]*MatrixConnection ) func New(cc *context.Context) { - c = cc + ctx = cc connections = make(map[string]*MatrixConnection) mp := MatrixPusher{} - c.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp)) + ctx.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp)) } diff --git a/pushers/matrix/matrixconnection.go b/pushers/matrix/matrixconnection.go index 8c3c006..9a5e476 100644 --- a/pushers/matrix/matrixconnection.go +++ b/pushers/matrix/matrixconnection.go @@ -64,14 +64,14 @@ type MatrixConnection struct { // nolint func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte, error) { - c.Log.Debug().Msgf("Data to send: %+v", data) + ctx.Log.Debug().Msgf("Data to send: %+v", data) apiRoot := mxc.apiRoot + endpoint if mxc.token != "" { apiRoot += fmt.Sprintf("?access_token=%s", mxc.token) } - c.Log.Debug().Msgf("Request URL: %s", apiRoot) + ctx.Log.Debug().Msgf("Request URL: %s", apiRoot) req, _ := http.NewRequest("POST", apiRoot, bytes.NewBuffer([]byte(data))) req.Header.Set("Content-Type", "application/json") @@ -97,14 +97,14 @@ func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte // nolint func (mxc *MatrixConnection) doPutRequest(endpoint string, data string) ([]byte, error) { - c.Log.Debug().Msgf("Data to send: %+v", data) + ctx.Log.Debug().Msgf("Data to send: %+v", data) apiRoot := mxc.apiRoot + endpoint if mxc.token != "" { apiRoot += fmt.Sprintf("?access_token=%s", mxc.token) } - c.Log.Debug().Msgf("Request URL: %s", apiRoot) + ctx.Log.Debug().Msgf("Request URL: %s", apiRoot) req, _ := http.NewRequest("PUT", apiRoot, bytes.NewBuffer([]byte(data))) req.Header.Set("Content-Type", "application/json") @@ -160,7 +160,7 @@ func (mxc *MatrixConnection) generateTnxIDSecureBytes(length int) []byte { randomBytes := make([]byte, length) if _, err := crand.Read(randomBytes); err != nil { - c.Log.Fatal().Msg("Unable to generate random bytes for transaction ID!") + ctx.Log.Fatal().Msg("Unable to generate random bytes for transaction ID!") } return randomBytes @@ -174,15 +174,15 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st mxc.roomID = roomID mxc.token = "" - c.Log.Debug().Str("conn", mxc.connName).Str("api_root", apiRoot).Msg("Trying to connect server") + ctx.Log.Debug().Str("conn", mxc.connName).Str("api_root", apiRoot).Msg("Trying to connect server") loginStr := fmt.Sprintf(`{"type": "m.login.password", "user": "%s", "password": "%s"}`, mxc.username, mxc.password) - c.Log.Debug().Msgf("Login string: %s", loginStr) + ctx.Log.Debug().Msgf("Login string: %s", loginStr) reply, err := mxc.doPostRequest("/login", loginStr) if err != nil { - c.Log.Fatal().Msgf("Failed to login to Matrix with user '%s' (conn %s): '%s'", mxc.username, mxc.connName, err.Error()) + ctx.Log.Fatal().Msgf("Failed to login to Matrix with user '%s' (conn %s): '%s'", mxc.username, mxc.connName, err.Error()) } // Parse received JSON and get access token. @@ -190,7 +190,7 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st err1 := json.Unmarshal(reply, &data) if err1 != nil { - c.Log.Fatal(). + ctx.Log.Fatal(). Str("username", mxc.username). Str("conn", mxc.connName). Err(err). @@ -200,14 +200,14 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st mxc.token, _ = data["access_token"].(string) mxc.deviceID, _ = data["deviceID"].(string) - c.Log.Debug().Str("conn", mxc.connName).Str("access_token", mxc.token).Str("device_id", mxc.deviceID).Msg("Login successful") + ctx.Log.Debug().Str("conn", mxc.connName).Str("access_token", mxc.token).Str("device_id", mxc.deviceID).Msg("Login successful") // We should check if we're already in room and, if not, join it. // We will do this by simply trying to join. We don't care about reply // here. _, err2 := mxc.doPostRequest("/rooms/"+mxc.roomID+"/join", "{}") if err2 != nil { - c.Log.Fatal().Err(err).Msg("Failed to join room") + ctx.Log.Fatal().Err(err).Msg("Failed to join room") } } @@ -215,7 +215,7 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st // It will prepare a message which will be passed to mxc.SendMessage(). func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) { // Prepare message body. - messageData := c.SendToParser(message.Username, message) + messageData := ctx.SendToParser(message.Username, message) messageToSend, _ := messageData["message"].(string) // We'll use HTML, so reformat links accordingly (if any). @@ -230,7 +230,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) { // "\n" should be "
". messageToSend = strings.ReplaceAll(messageToSend, "\n", "
") - c.Log.Debug().Msgf("Crafted message: %s", messageToSend) + ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend) // Send message. mxc.SendMessage(messageToSend) @@ -238,7 +238,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) { // This function sends already prepared message to room. func (mxc *MatrixConnection) SendMessage(message string) { - c.Log.Debug().Str("conn", mxc.connName).Msgf("Sending message: '%s'", message) + ctx.Log.Debug().Str("conn", mxc.connName).Msgf("Sending message: '%s'", message) // We should send notices as it is preferred behavior for bots and // appservices. @@ -251,7 +251,7 @@ func (mxc *MatrixConnection) SendMessage(message string) { msgBytes, err := json.Marshal(&msg) if err != nil { - c.Log.Error().Err(err).Msg("Failed to marshal message into JSON.") + ctx.Log.Error().Err(err).Msg("Failed to marshal message into JSON.") return } @@ -260,20 +260,20 @@ func (mxc *MatrixConnection) SendMessage(message string) { reply, err := mxc.doPutRequest("/rooms/"+mxc.roomID+"/send/m.room.message/"+mxc.generateTnxID(), msgStr) if err != nil { - c.Log.Fatal().Str("conn", mxc.connName).Str("room", mxc.roomID).Err(err).Msg("Failed to send message to room") + ctx.Log.Fatal().Str("conn", mxc.connName).Str("room", mxc.roomID).Err(err).Msg("Failed to send message to room") } - c.Log.Debug().Msgf("Message sent, reply: %s", string(reply)) + ctx.Log.Debug().Msgf("Message sent, reply: %s", string(reply)) } func (mxc *MatrixConnection) Shutdown() { - c.Log.Info().Str("conn", mxc.connName).Msg("Shutting down connection...") + ctx.Log.Info().Str("conn", mxc.connName).Msg("Shutting down connection...") _, err := mxc.doPostRequest("/logout", "{}") if err != nil { - c.Log.Error().Err(err).Str("conn", mxc.connName).Msg("Error occurred while trying to log out from Matrix.") + ctx.Log.Error().Err(err).Str("conn", mxc.connName).Msg("Error occurred while trying to log out from Matrix.") } mxc.token = "" - c.Log.Info().Str("conn", mxc.connName).Msg("Connection successfully shutted down") + ctx.Log.Info().Str("conn", mxc.connName).Msg("Connection successfully shutted down") } diff --git a/pushers/matrix/matrixpusher.go b/pushers/matrix/matrixpusher.go index 0f798bf..1d0d44f 100644 --- a/pushers/matrix/matrixpusher.go +++ b/pushers/matrix/matrixpusher.go @@ -22,15 +22,15 @@ import slackmessage "go.dev.pztrn.name/opensaps/slack/message" type MatrixPusher struct{} func (mp MatrixPusher) Initialize() { - c.Log.Info().Msg("Initializing Matrix protocol pusher...") + ctx.Log.Info().Msg("Initializing Matrix protocol pusher...") // Get configuration for pushers and initialize every connection. - cfg := c.Config.GetConfig() + cfg := ctx.Config.GetConfig() for name, config := range cfg.Matrix { - c.Log.Info().Str("conn", name).Msg("Initializing connection...") + ctx.Log.Info().Str("conn", name).Msg("Initializing connection...") // Fields will be filled with conn.Initialize(). - // nolint:exhaustivestruct + // nolint:exhaustruct conn := MatrixConnection{} connections[name] = &conn @@ -41,17 +41,17 @@ func (mp MatrixPusher) Initialize() { func (mp MatrixPusher) Push(connection string, data slackmessage.SlackMessage) { conn, found := connections[connection] if !found { - c.Log.Error().Str("conn", connection).Msg("Connection not found!") + ctx.Log.Error().Str("conn", connection).Msg("Connection not found!") return } - c.Log.Debug().Str("conn", connection).Msg("Pushing data to connection") + ctx.Log.Debug().Str("conn", connection).Msg("Pushing data to connection") conn.ProcessMessage(data) } func (mp MatrixPusher) Shutdown() { - c.Log.Info().Msg("Shutting down Matrix pusher...") + ctx.Log.Info().Msg("Shutting down Matrix pusher...") for _, conn := range connections { conn.Shutdown() diff --git a/pushers/telegram/exported.go b/pushers/telegram/exported.go index 7760b4d..f64b69f 100644 --- a/pushers/telegram/exported.go +++ b/pushers/telegram/exported.go @@ -23,14 +23,14 @@ import ( ) var ( - c *context.Context + ctx *context.Context connections map[string]*TelegramConnection ) func New(cc *context.Context) { - c = cc + ctx = cc connections = make(map[string]*TelegramConnection) tp := TelegramPusher{} - c.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp)) + ctx.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp)) } diff --git a/pushers/telegram/telegramconnection.go b/pushers/telegram/telegramconnection.go index 9a27e0a..3450f89 100644 --- a/pushers/telegram/telegramconnection.go +++ b/pushers/telegram/telegramconnection.go @@ -37,7 +37,7 @@ func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.Confi func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) { // Prepare message body. - messageData := c.SendToParser(message.Username, message) + messageData := ctx.SendToParser(message.Username, message) messageToSend, _ := messageData["message"].(string) // We'll use HTML, so reformat links accordingly (if any). @@ -49,7 +49,7 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) } } - c.Log.Debug().Msgf("Crafted message: %s", messageToSend) + ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend) // Send message. tc.SendMessage(messageToSend) @@ -62,7 +62,7 @@ func (tc *TelegramConnection) SendMessage(message string) { msgdata.Set("parse_mode", "HTML") // Are we should use proxy? - // nolint:exhaustivestruct + // nolint:exhaustruct httpTransport := &http.Transport{} // nolint:nestif @@ -82,30 +82,30 @@ func (tc *TelegramConnection) SendMessage(message string) { proxyURLParsed, err := url.Parse(proxyURL) if err != nil { - c.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL") + ctx.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL") } else { httpTransport.Proxy = http.ProxyURL(proxyURLParsed) } } - // nolint:exhaustivestruct + // nolint:exhaustruct client := &http.Client{Transport: httpTransport} botURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID) - c.Log.Debug().Msgf("Bot URL: %s", botURL) + ctx.Log.Debug().Msgf("Bot URL: %s", botURL) // ToDo: fix it. // nolint response, err := client.PostForm(botURL, msgdata) if err != nil { - c.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram") + ctx.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram") } else { - c.Log.Debug().Msgf("Status: %s", response.Status) + ctx.Log.Debug().Msgf("Status: %s", response.Status) if response.StatusCode != http.StatusOK { body := []byte{} _, _ = response.Body.Read(body) response.Body.Close() - c.Log.Debug().Msg(string(body)) + ctx.Log.Debug().Msg(string(body)) } } } diff --git a/pushers/telegram/telegrampusher.go b/pushers/telegram/telegrampusher.go index 6b46309..70dd156 100644 --- a/pushers/telegram/telegrampusher.go +++ b/pushers/telegram/telegrampusher.go @@ -24,14 +24,14 @@ import ( type TelegramPusher struct{} func (tp TelegramPusher) Initialize() { - c.Log.Info().Msg("Initializing Telegram protocol pusher...") + ctx.Log.Info().Msg("Initializing Telegram protocol pusher...") // Get configuration for pushers and initialize every connection. - cfg := c.Config.GetConfig() + cfg := ctx.Config.GetConfig() for name, config := range cfg.Telegram { - c.Log.Info().Str("conn", name).Msg("Initializing connection...") + ctx.Log.Info().Str("conn", name).Msg("Initializing connection...") - // nolint:exhaustivestruct + // nolint:exhaustruct conn := TelegramConnection{} connections[name] = &conn @@ -42,17 +42,17 @@ func (tp TelegramPusher) Initialize() { func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) { conn, found := connections[connection] if !found { - c.Log.Error().Str("conn", connection).Msg("Connection not found") + ctx.Log.Error().Str("conn", connection).Msg("Connection not found") return } - c.Log.Debug().Str("conn", connection).Msg("Pushing data") + ctx.Log.Debug().Str("conn", connection).Msg("Pushing data") conn.ProcessMessage(data) } func (tp TelegramPusher) Shutdown() { - c.Log.Info().Msg("Shutting down Telegram pusher...") + ctx.Log.Info().Msg("Shutting down Telegram pusher...") for _, conn := range connections { conn.Shutdown() diff --git a/slack/exported.go b/slack/exported.go index e78247d..06a18ba 100644 --- a/slack/exported.go +++ b/slack/exported.go @@ -25,13 +25,13 @@ import ( ) var ( - c *context.Context + ctx *context.Context // HTTP server. httpsrv *http.Server ) func New(cc *context.Context) { - c = cc + ctx = cc sh := APIServer{} - c.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh)) + ctx.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh)) } diff --git a/slack/slackapiserver.go b/slack/slackapiserver.go index a5a8c95..d1d8c15 100644 --- a/slack/slackapiserver.go +++ b/slack/slackapiserver.go @@ -37,16 +37,16 @@ import ( type APIServer struct{} func (sh APIServer) Initialize() { - c.Log.Info().Msg("Initializing Slack API handler...") + ctx.Log.Info().Msg("Initializing Slack API handler...") // Start HTTP server. // As OpenSAPS designed to be behind some proxy (nginx, Caddy, etc.) // we will listen only to plain HTTP. // Note to those who wants HTTPS - proxify with nginx, Caddy, etc! // Don't send pull requests, patches, don't create issues! :) - cfg := c.Config.GetConfig() + cfg := ctx.Config.GetConfig() - // nolint:exhaustivestruct,gomnd + // nolint:exhaustruct,gomnd httpsrv = &http.Server{ Addr: cfg.SlackHandler.Listener.Address, // This handler will figure out from where request has come and will @@ -62,13 +62,13 @@ func (sh APIServer) Initialize() { _ = httpsrv.ListenAndServe() }() - c.Log.Info().Str("address", cfg.SlackHandler.Listener.Address).Msg("Starting Slack Webhooks API server") + ctx.Log.Info().Str("address", cfg.SlackHandler.Listener.Address).Msg("Starting Slack Webhooks API server") } func (sh APIServer) Shutdown() { - c.Log.Info().Msg("Shutting down Slack API handler...") + ctx.Log.Info().Msg("Shutting down Slack API handler...") _ = httpsrv.Shutdown(context.TODO()) - c.Log.Info().Msg("Slack API HTTP server shutted down") + ctx.Log.Info().Msg("Slack API HTTP server shutted down") } diff --git a/slack/slackhandler.go b/slack/slackhandler.go index 1a6ad47..c57454e 100644 --- a/slack/slackhandler.go +++ b/slack/slackhandler.go @@ -31,11 +31,11 @@ import ( type Handler struct{} func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { - c.Log.Debug().Str("method", req.Method).Str("host", req.Host).Str("path", req.URL.Path).Msg("Received HTTP request") + ctx.Log.Debug().Str("method", req.Method).Str("host", req.Host).Str("path", req.URL.Path).Msg("Received HTTP request") // We should catch only POST requests. Otherwise return HTTP 404. if req.Method != "POST" { - c.Log.Debug().Msg("Not a POST request, returning HTTP 404") + ctx.Log.Debug().Msg("Not a POST request, returning HTTP 404") respwriter.WriteHeader(http.StatusNotFound) fmt.Fprintf(respwriter, "NOT FOUND") @@ -45,10 +45,10 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { body, _ := ioutil.ReadAll(req.Body) req.Body.Close() - c.Log.Debug().Msgf("Received body: %s", string(body)) + ctx.Log.Debug().Msgf("Received body: %s", string(body)) // Try to figure out where we should push received data. - cfg := c.Config.GetConfig() + cfg := ctx.Config.GetConfig() var sentToPusher bool @@ -56,7 +56,7 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { if strings.Contains(req.URL.Path, config.Slack.Random1) && strings.Contains(req.URL.Path, config.Slack.Random2) && strings.Contains(req.URL.Path, config.Slack.LongRandom) { - c.Log.Debug().Msgf("Passed data belongs to '%s' and should go to '%s' pusher, protocol '%s'", + ctx.Log.Debug().Msgf("Passed data belongs to '%s' and should go to '%s' pusher, protocol '%s'", name, config.Remote.PushTo, config.Remote.Pusher) // Parse message into SlackMessage structure. if strings.Contains(string(body)[0:7], "payload") { @@ -69,7 +69,7 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { // Second - unescape data. tempBody, err := url.QueryUnescape(tempBody) if err != nil { - c.Log.Error().Msg("Failed to decode body into parseable string!") + ctx.Log.Error().Msg("Failed to decode body into parseable string!") return } @@ -78,25 +78,25 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { body = []byte(tempBody) } - // nolint:exhaustivestruct + // nolint:exhaustruct slackmsg := slackmessage.SlackMessage{} err := json.Unmarshal(body, &slackmsg) if err != nil { - c.Log.Error().Err(err).Msg("Failed to decode JSON into SlackMessage struct") + ctx.Log.Error().Err(err).Msg("Failed to decode JSON into SlackMessage struct") return } - c.Log.Debug().Msgf("Received message: %+v", slackmsg) - c.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg) + ctx.Log.Debug().Msgf("Received message: %+v", slackmsg) + ctx.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg) sentToPusher = true } } if !sentToPusher { - c.Log.Debug().Msg("Don't know where to push data. Ignoring with HTTP 404") + ctx.Log.Debug().Msg("Don't know where to push data. Ignoring with HTTP 404") respwriter.WriteHeader(http.StatusNotFound) fmt.Fprintf(respwriter, "NOT FOUND") }