Moved to zerolog, linting fixes, update Drone configuration.

This commit is contained in:
2021-11-21 16:16:10 +05:00
parent c6c3e2e76f
commit 46aee4d2ab
27 changed files with 258 additions and 169 deletions

View File

@@ -18,10 +18,8 @@
package slack
import (
// stdlib
"net/http"
// local
"go.dev.pztrn.name/opensaps/context"
slackapiserverinterface "go.dev.pztrn.name/opensaps/slack/apiserverinterface"
)

View File

@@ -17,14 +17,15 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package slackmessage
// nolint:tagliatelle
type SlackMessage struct {
Channel string `json:"channel"`
Text string `json:"text"`
Username string `json:"username"`
IconURL string `json:"icon_url"`
Attachments []SlackAttachments `json:"attachments"`
UnfurlLinks int `json:"unfurl_links"`
LinkNames int `json:"link_names"`
Attachments []SlackAttachments `json:"attachments"`
}
type SlackAttachments struct {

View File

@@ -29,8 +29,6 @@ package slack
// handler for it.
import (
// stdlib
"context"
"net/http"
"time"
@@ -39,7 +37,7 @@ import (
type APIServer struct{}
func (sh APIServer) Initialize() {
c.Log.Infoln("Initializing Slack API handler...")
c.Log.Info().Msg("Initializing Slack API handler...")
// Start HTTP server.
// As OpenSAPS designed to be behind some proxy (nginx, Caddy, etc.)
@@ -48,6 +46,7 @@ func (sh APIServer) Initialize() {
// Don't send pull requests, patches, don't create issues! :)
cfg := c.Config.GetConfig()
// nolint:exhaustivestruct,gomnd
httpsrv = &http.Server{
Addr: cfg.SlackHandler.Listener.Address,
// This handler will figure out from where request has come and will
@@ -63,13 +62,13 @@ func (sh APIServer) Initialize() {
_ = httpsrv.ListenAndServe()
}()
c.Log.Infof("Slack Webhooks API server starting to listen on %s", cfg.SlackHandler.Listener.Address)
c.Log.Info().Str("address", cfg.SlackHandler.Listener.Address).Msg("Starting Slack Webhooks API server")
}
func (sh APIServer) Shutdown() {
c.Log.Infoln("Shutting down Slack API handler...")
c.Log.Info().Msg("Shutting down Slack API handler...")
_ = httpsrv.Shutdown(context.TODO())
c.Log.Infoln("Slack API HTTP server shutted down")
c.Log.Info().Msg("Slack API HTTP server shutted down")
}

View File

@@ -18,7 +18,6 @@
package slack
import (
// stdlib
"encoding/json"
"fmt"
"io/ioutil"
@@ -26,19 +25,18 @@ import (
"net/url"
"strings"
// local
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
)
type Handler struct{}
func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
c.Log.Debugf("Received '%s' (empty = GET) request from %s, URL: '%s'", req.Method, req.Host, req.URL.Path)
c.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.Debugln("Not a POST request, returning HTTP 404")
respwriter.WriteHeader(404)
c.Log.Debug().Msg("Not a POST request, returning HTTP 404")
respwriter.WriteHeader(http.StatusNotFound)
fmt.Fprintf(respwriter, "NOT FOUND")
return
@@ -47,18 +45,18 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
req.Body.Close()
c.Log.Debugf("Received body: %s", string(body))
c.Log.Debug().Msgf("Received body: %s", string(body))
// Try to figure out where we should push received data.
cfg := c.Config.GetConfig()
var sentToPusher bool = false
var sentToPusher bool
for name, config := range cfg.Webhooks {
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.Debugf("Passed data belongs to '%s' and should go to '%s' pusher, protocol '%s'",
c.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") {
@@ -71,7 +69,8 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
// Second - unescape data.
tempBody, err := url.QueryUnescape(tempBody)
if err != nil {
c.Log.Errorln("Failed to decode body into parseable string!")
c.Log.Error().Msg("Failed to decode body into parseable string!")
return
}
@@ -79,15 +78,17 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
body = []byte(tempBody)
}
// nolint:exhaustivestruct
slackmsg := slackmessage.SlackMessage{}
err := json.Unmarshal(body, &slackmsg)
if err != nil {
c.Log.Errorf("Failed to decode JSON into SlackMessage struct: '%s'\n", err.Error())
c.Log.Error().Err(err).Msg("Failed to decode JSON into SlackMessage struct")
return
}
c.Log.Debug("Received message:", fmt.Sprintf("%+v", slackmsg))
c.Log.Debug().Msgf("Received message: %+v", slackmsg)
c.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg)
sentToPusher = true
@@ -95,8 +96,8 @@ func (sh Handler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
}
if !sentToPusher {
c.Log.Debug("Don't know where to push data. Ignoring with HTTP 404")
respwriter.WriteHeader(404)
c.Log.Debug().Msg("Don't know where to push data. Ignoring with HTTP 404")
respwriter.WriteHeader(http.StatusNotFound)
fmt.Fprintf(respwriter, "NOT FOUND")
}
}