Make linters happy.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Stanislav Nikitin 2022-06-29 13:16:43 +05:00
parent 24644dba93
commit bff1050eab
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550
14 changed files with 100 additions and 97 deletions

View File

@ -8,6 +8,8 @@ linters:
- gochecknoglobals - gochecknoglobals
# This linter goes crazy for nothing (almost). # This linter goes crazy for nothing (almost).
- funlen - funlen
# Deprecated things.
- exhaustivestruct
linters-settings: linters-settings:
lll: lll:
line-length: 128 line-length: 128

View File

@ -50,7 +50,7 @@ func (conf Configuration) GetTempValue(key string) (string, error) {
if value[0] == '~' { if value[0] == '~' {
usr, err := user.Current() usr, err := user.Current()
if err != nil { 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) value = strings.Replace(value, "~", usr.HomeDir, 1)
@ -60,7 +60,7 @@ func (conf Configuration) GetTempValue(key string) (string, error) {
} }
func (conf Configuration) Initialize() { func (conf Configuration) Initialize() {
c.Log.Info().Msg("Initializing configuration storage...") ctx.Log.Info().Msg("Initializing configuration storage...")
tempconfig = make(map[string]string) tempconfig = make(map[string]string)
@ -71,26 +71,26 @@ func (conf Configuration) Initialize() {
DefaultValue: "~/.config/OpenSAPS/config.yaml", DefaultValue: "~/.config/OpenSAPS/config.yaml",
} }
_ = c.Flagger.AddFlag(&flagConfigpath) _ = ctx.Flagger.AddFlag(&flagConfigpath)
} }
// Initializes configuration root path for later usage. // Initializes configuration root path for later usage.
func (conf Configuration) initializeConfigurationFilePath() { 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 { 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) conf.SetTempValue("CONFIGURATION_FILE", configpath)
} }
// Asking Flagger about flags, initialize internal variables. // Asking Flagger about flags, initialize internal variables.
// Should be called **after** Flagger.Parse(). // Should be called **after** Flagger.Parse().
func (conf Configuration) InitializeLater() { func (conf Configuration) InitializeLater() {
c.Log.Info().Msg("Completing configuration initialization...") ctx.Log.Info().Msg("Completing configuration initialization...")
conf.initializeConfigurationFilePath() conf.initializeConfigurationFilePath()
} }
@ -99,26 +99,27 @@ func (conf Configuration) InitializeLater() {
func (conf Configuration) LoadConfigurationFromFile() { func (conf Configuration) LoadConfigurationFromFile() {
configpath, err := conf.GetTempValue("CONFIGURATION_FILE") configpath, err := conf.GetTempValue("CONFIGURATION_FILE")
if err != nil { 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. // Read file into memory.
configBytes, err1 := ioutil.ReadFile(configpath) configBytes, err1 := ioutil.ReadFile(configpath)
if err1 != nil { 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{} config = &configstruct.ConfigStruct{}
// Parse YAML. // Parse YAML.
err2 := yaml.Unmarshal(configBytes, config) err2 := yaml.Unmarshal(configBytes, config)
if err2 != nil { 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. // Sets value to key in temporary configuration storage.

View File

@ -24,7 +24,7 @@ import (
) )
var ( var (
c *context.Context ctx *context.Context
// Temporary configuration. // Temporary configuration.
tempconfig map[string]string tempconfig map[string]string
// Configuration from YAML file. // Configuration from YAML file.
@ -32,7 +32,7 @@ var (
) )
func New(cc *context.Context) { func New(cc *context.Context) {
c = cc ctx = cc
conf := Configuration{} conf := Configuration{}
c.RegisterConfigurationInterface(configurationinterface.ConfigurationInterface(conf)) ctx.RegisterConfigurationInterface(configurationinterface.ConfigurationInterface(conf))
} }

View File

@ -45,32 +45,32 @@ func (c *Context) Initialize() {
c.Parsers = make(map[string]parserinterface.ParserInterface) c.Parsers = make(map[string]parserinterface.ParserInterface)
c.Pushers = make(map[string]pusherinterface.PusherInterface) c.Pushers = make(map[string]pusherinterface.PusherInterface)
// nolint:exhaustivestruct // nolint:exhaustruct
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339} output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(lvlRaw interface{}) string { output.FormatLevel = func(lvlRaw interface{}) string {
var v string var formattedLvl string
if lvl, ok := lvlRaw.(string); ok { if lvl, ok := lvlRaw.(string); ok {
lvl = strings.ToUpper(lvl) lvl = strings.ToUpper(lvl)
switch lvl { switch lvl {
case "DEBUG": case "DEBUG":
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
case "ERROR": case "ERROR":
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
case "FATAL": case "FATAL":
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
case "INFO": case "INFO":
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
case "PANIC": case "PANIC":
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
case "WARN": case "WARN":
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl) formattedLvl = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
default: default:
v = lvl formattedLvl = lvl
} }
} }
return fmt.Sprintf("| %s |", v) return fmt.Sprintf("| %s |", formattedLvl)
} }
c.Log = zerolog.New(output).With().Timestamp().Logger() c.Log = zerolog.New(output).With().Timestamp().Logger()

View File

@ -18,6 +18,6 @@
package context package context
func New() *Context { func New() *Context {
// nolint:exhaustivestruct // nolint:exhaustruct
return &Context{} return &Context{}
} }

View File

@ -23,14 +23,14 @@ import (
) )
var ( var (
c *context.Context ctx *context.Context
connections map[string]*MatrixConnection connections map[string]*MatrixConnection
) )
func New(cc *context.Context) { func New(cc *context.Context) {
c = cc ctx = cc
connections = make(map[string]*MatrixConnection) connections = make(map[string]*MatrixConnection)
mp := MatrixPusher{} mp := MatrixPusher{}
c.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp)) ctx.RegisterPusherInterface("matrix", pusherinterface.PusherInterface(mp))
} }

View File

@ -64,14 +64,14 @@ type MatrixConnection struct {
// nolint // nolint
func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte, error) { 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 apiRoot := mxc.apiRoot + endpoint
if mxc.token != "" { if mxc.token != "" {
apiRoot += fmt.Sprintf("?access_token=%s", 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, _ := http.NewRequest("POST", apiRoot, bytes.NewBuffer([]byte(data)))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -97,14 +97,14 @@ func (mxc *MatrixConnection) doPostRequest(endpoint string, data string) ([]byte
// nolint // nolint
func (mxc *MatrixConnection) doPutRequest(endpoint string, data string) ([]byte, error) { 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 apiRoot := mxc.apiRoot + endpoint
if mxc.token != "" { if mxc.token != "" {
apiRoot += fmt.Sprintf("?access_token=%s", 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, _ := http.NewRequest("PUT", apiRoot, bytes.NewBuffer([]byte(data)))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -160,7 +160,7 @@ func (mxc *MatrixConnection) generateTnxIDSecureBytes(length int) []byte {
randomBytes := make([]byte, length) randomBytes := make([]byte, length)
if _, err := crand.Read(randomBytes); err != nil { 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 return randomBytes
@ -174,15 +174,15 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
mxc.roomID = roomID mxc.roomID = roomID
mxc.token = "" 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) 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) reply, err := mxc.doPostRequest("/login", loginStr)
if err != nil { 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. // 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) err1 := json.Unmarshal(reply, &data)
if err1 != nil { if err1 != nil {
c.Log.Fatal(). ctx.Log.Fatal().
Str("username", mxc.username). Str("username", mxc.username).
Str("conn", mxc.connName). Str("conn", mxc.connName).
Err(err). Err(err).
@ -200,14 +200,14 @@ func (mxc *MatrixConnection) Initialize(connName string, apiRoot string, user st
mxc.token, _ = data["access_token"].(string) mxc.token, _ = data["access_token"].(string)
mxc.deviceID, _ = data["deviceID"].(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 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 // We will do this by simply trying to join. We don't care about reply
// here. // here.
_, err2 := mxc.doPostRequest("/rooms/"+mxc.roomID+"/join", "{}") _, err2 := mxc.doPostRequest("/rooms/"+mxc.roomID+"/join", "{}")
if err2 != nil { 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(). // It will prepare a message which will be passed to mxc.SendMessage().
func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) { func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
// Prepare message body. // Prepare message body.
messageData := c.SendToParser(message.Username, message) messageData := ctx.SendToParser(message.Username, message)
messageToSend, _ := messageData["message"].(string) messageToSend, _ := messageData["message"].(string)
// We'll use HTML, so reformat links accordingly (if any). // We'll use HTML, so reformat links accordingly (if any).
@ -230,7 +230,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
// "\n" should be "<br>". // "\n" should be "<br>".
messageToSend = strings.ReplaceAll(messageToSend, "\n", "<br>") messageToSend = strings.ReplaceAll(messageToSend, "\n", "<br>")
c.Log.Debug().Msgf("Crafted message: %s", messageToSend) ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend)
// Send message. // Send message.
mxc.SendMessage(messageToSend) mxc.SendMessage(messageToSend)
@ -238,7 +238,7 @@ func (mxc *MatrixConnection) ProcessMessage(message slackmessage.SlackMessage) {
// This function sends already prepared message to room. // This function sends already prepared message to room.
func (mxc *MatrixConnection) SendMessage(message string) { 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 // We should send notices as it is preferred behavior for bots and
// appservices. // appservices.
@ -251,7 +251,7 @@ func (mxc *MatrixConnection) SendMessage(message string) {
msgBytes, err := json.Marshal(&msg) msgBytes, err := json.Marshal(&msg)
if err != nil { 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 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) reply, err := mxc.doPutRequest("/rooms/"+mxc.roomID+"/send/m.room.message/"+mxc.generateTnxID(), msgStr)
if err != nil { 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() { 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", "{}") _, err := mxc.doPostRequest("/logout", "{}")
if err != nil { 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 = "" 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")
} }

View File

@ -22,15 +22,15 @@ import slackmessage "go.dev.pztrn.name/opensaps/slack/message"
type MatrixPusher struct{} type MatrixPusher struct{}
func (mp MatrixPusher) Initialize() { 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. // Get configuration for pushers and initialize every connection.
cfg := c.Config.GetConfig() cfg := ctx.Config.GetConfig()
for name, config := range cfg.Matrix { 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(). // Fields will be filled with conn.Initialize().
// nolint:exhaustivestruct // nolint:exhaustruct
conn := MatrixConnection{} conn := MatrixConnection{}
connections[name] = &conn connections[name] = &conn
@ -41,17 +41,17 @@ func (mp MatrixPusher) Initialize() {
func (mp MatrixPusher) Push(connection string, data slackmessage.SlackMessage) { func (mp MatrixPusher) Push(connection string, data slackmessage.SlackMessage) {
conn, found := connections[connection] conn, found := connections[connection]
if !found { if !found {
c.Log.Error().Str("conn", connection).Msg("Connection not found!") ctx.Log.Error().Str("conn", connection).Msg("Connection not found!")
return 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) conn.ProcessMessage(data)
} }
func (mp MatrixPusher) Shutdown() { func (mp MatrixPusher) Shutdown() {
c.Log.Info().Msg("Shutting down Matrix pusher...") ctx.Log.Info().Msg("Shutting down Matrix pusher...")
for _, conn := range connections { for _, conn := range connections {
conn.Shutdown() conn.Shutdown()

View File

@ -23,14 +23,14 @@ import (
) )
var ( var (
c *context.Context ctx *context.Context
connections map[string]*TelegramConnection connections map[string]*TelegramConnection
) )
func New(cc *context.Context) { func New(cc *context.Context) {
c = cc ctx = cc
connections = make(map[string]*TelegramConnection) connections = make(map[string]*TelegramConnection)
tp := TelegramPusher{} tp := TelegramPusher{}
c.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp)) ctx.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp))
} }

View File

@ -37,7 +37,7 @@ func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.Confi
func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) { func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) {
// Prepare message body. // Prepare message body.
messageData := c.SendToParser(message.Username, message) messageData := ctx.SendToParser(message.Username, message)
messageToSend, _ := messageData["message"].(string) messageToSend, _ := messageData["message"].(string)
// We'll use HTML, so reformat links accordingly (if any). // 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. // Send message.
tc.SendMessage(messageToSend) tc.SendMessage(messageToSend)
@ -62,7 +62,7 @@ func (tc *TelegramConnection) SendMessage(message string) {
msgdata.Set("parse_mode", "HTML") msgdata.Set("parse_mode", "HTML")
// Are we should use proxy? // Are we should use proxy?
// nolint:exhaustivestruct // nolint:exhaustruct
httpTransport := &http.Transport{} httpTransport := &http.Transport{}
// nolint:nestif // nolint:nestif
@ -82,30 +82,30 @@ func (tc *TelegramConnection) SendMessage(message string) {
proxyURLParsed, err := url.Parse(proxyURL) proxyURLParsed, err := url.Parse(proxyURL)
if err != nil { 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 { } else {
httpTransport.Proxy = http.ProxyURL(proxyURLParsed) httpTransport.Proxy = http.ProxyURL(proxyURLParsed)
} }
} }
// nolint:exhaustivestruct // nolint:exhaustruct
client := &http.Client{Transport: httpTransport} client := &http.Client{Transport: httpTransport}
botURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID) 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. // ToDo: fix it.
// nolint // nolint
response, err := client.PostForm(botURL, msgdata) response, err := client.PostForm(botURL, msgdata)
if err != nil { 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 { } else {
c.Log.Debug().Msgf("Status: %s", response.Status) ctx.Log.Debug().Msgf("Status: %s", response.Status)
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
body := []byte{} body := []byte{}
_, _ = response.Body.Read(body) _, _ = response.Body.Read(body)
response.Body.Close() response.Body.Close()
c.Log.Debug().Msg(string(body)) ctx.Log.Debug().Msg(string(body))
} }
} }
} }

View File

@ -24,14 +24,14 @@ import (
type TelegramPusher struct{} type TelegramPusher struct{}
func (tp TelegramPusher) Initialize() { 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. // Get configuration for pushers and initialize every connection.
cfg := c.Config.GetConfig() cfg := ctx.Config.GetConfig()
for name, config := range cfg.Telegram { 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{} conn := TelegramConnection{}
connections[name] = &conn connections[name] = &conn
@ -42,17 +42,17 @@ func (tp TelegramPusher) Initialize() {
func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) { func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) {
conn, found := connections[connection] conn, found := connections[connection]
if !found { if !found {
c.Log.Error().Str("conn", connection).Msg("Connection not found") ctx.Log.Error().Str("conn", connection).Msg("Connection not found")
return return
} }
c.Log.Debug().Str("conn", connection).Msg("Pushing data") ctx.Log.Debug().Str("conn", connection).Msg("Pushing data")
conn.ProcessMessage(data) conn.ProcessMessage(data)
} }
func (tp TelegramPusher) Shutdown() { func (tp TelegramPusher) Shutdown() {
c.Log.Info().Msg("Shutting down Telegram pusher...") ctx.Log.Info().Msg("Shutting down Telegram pusher...")
for _, conn := range connections { for _, conn := range connections {
conn.Shutdown() conn.Shutdown()

View File

@ -25,13 +25,13 @@ import (
) )
var ( var (
c *context.Context ctx *context.Context
// HTTP server. // HTTP server.
httpsrv *http.Server httpsrv *http.Server
) )
func New(cc *context.Context) { func New(cc *context.Context) {
c = cc ctx = cc
sh := APIServer{} sh := APIServer{}
c.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh)) ctx.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh))
} }

View File

@ -37,16 +37,16 @@ import (
type APIServer struct{} type APIServer struct{}
func (sh APIServer) Initialize() { func (sh APIServer) Initialize() {
c.Log.Info().Msg("Initializing Slack API handler...") ctx.Log.Info().Msg("Initializing Slack API handler...")
// Start HTTP server. // Start HTTP server.
// As OpenSAPS designed to be behind some proxy (nginx, Caddy, etc.) // As OpenSAPS designed to be behind some proxy (nginx, Caddy, etc.)
// we will listen only to plain HTTP. // we will listen only to plain HTTP.
// Note to those who wants HTTPS - proxify with nginx, Caddy, etc! // Note to those who wants HTTPS - proxify with nginx, Caddy, etc!
// Don't send pull requests, patches, don't create issues! :) // 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{ httpsrv = &http.Server{
Addr: cfg.SlackHandler.Listener.Address, Addr: cfg.SlackHandler.Listener.Address,
// This handler will figure out from where request has come and will // This handler will figure out from where request has come and will
@ -62,13 +62,13 @@ func (sh APIServer) Initialize() {
_ = httpsrv.ListenAndServe() _ = 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() { 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()) _ = httpsrv.Shutdown(context.TODO())
c.Log.Info().Msg("Slack API HTTP server shutted down") ctx.Log.Info().Msg("Slack API HTTP server shutted down")
} }

View File

@ -31,11 +31,11 @@ import (
type Handler struct{} type Handler struct{}
func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) { 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. // We should catch only POST requests. Otherwise return HTTP 404.
if req.Method != "POST" { 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) respwriter.WriteHeader(http.StatusNotFound)
fmt.Fprintf(respwriter, "NOT FOUND") fmt.Fprintf(respwriter, "NOT FOUND")
@ -45,10 +45,10 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body) body, _ := ioutil.ReadAll(req.Body)
req.Body.Close() 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. // Try to figure out where we should push received data.
cfg := c.Config.GetConfig() cfg := ctx.Config.GetConfig()
var sentToPusher bool 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) && if strings.Contains(req.URL.Path, config.Slack.Random1) &&
strings.Contains(req.URL.Path, config.Slack.Random2) && strings.Contains(req.URL.Path, config.Slack.Random2) &&
strings.Contains(req.URL.Path, config.Slack.LongRandom) { 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) name, config.Remote.PushTo, config.Remote.Pusher)
// Parse message into SlackMessage structure. // Parse message into SlackMessage structure.
if strings.Contains(string(body)[0:7], "payload") { 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. // Second - unescape data.
tempBody, err := url.QueryUnescape(tempBody) tempBody, err := url.QueryUnescape(tempBody)
if err != nil { 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 return
} }
@ -78,25 +78,25 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
body = []byte(tempBody) body = []byte(tempBody)
} }
// nolint:exhaustivestruct // nolint:exhaustruct
slackmsg := slackmessage.SlackMessage{} slackmsg := slackmessage.SlackMessage{}
err := json.Unmarshal(body, &slackmsg) err := json.Unmarshal(body, &slackmsg)
if err != nil { 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 return
} }
c.Log.Debug().Msgf("Received message: %+v", slackmsg) ctx.Log.Debug().Msgf("Received message: %+v", slackmsg)
c.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg) ctx.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg)
sentToPusher = true sentToPusher = true
} }
} }
if !sentToPusher { 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) respwriter.WriteHeader(http.StatusNotFound)
fmt.Fprintf(respwriter, "NOT FOUND") fmt.Fprintf(respwriter, "NOT FOUND")
} }