Switching to modules, removed vendored dependencies, linting with golangci-lint.
This commit is contained in:
@@ -18,6 +18,6 @@
|
||||
package slackapiserverinterface
|
||||
|
||||
type SlackAPIServerInterface interface {
|
||||
Initialize()
|
||||
Shutdown()
|
||||
Initialize()
|
||||
Shutdown()
|
||||
}
|
||||
|
@@ -22,8 +22,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"gitlab.com/pztrn/opensaps/context"
|
||||
"gitlab.com/pztrn/opensaps/slack/apiserverinterface"
|
||||
"go.dev.pztrn.name/opensaps/context"
|
||||
slackapiserverinterface "go.dev.pztrn.name/opensaps/slack/apiserverinterface"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -34,6 +34,6 @@ var (
|
||||
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
sh := SlackAPIServer{}
|
||||
sh := APIServer{}
|
||||
c.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh))
|
||||
}
|
||||
|
@@ -18,18 +18,18 @@
|
||||
package slackmessage
|
||||
|
||||
type SlackMessage struct {
|
||||
Channel string `json:"channel"`
|
||||
Text string `json:"text"`
|
||||
Username string `json:"username"`
|
||||
IconURL string `json:"icon_url"`
|
||||
UnfurlLinks int `json:"unfurl_links"`
|
||||
LinkNames int `json:"link_names"`
|
||||
Attachments []SlackAttachments `json:"attachments"`
|
||||
Channel string `json:"channel"`
|
||||
Text string `json:"text"`
|
||||
Username string `json:"username"`
|
||||
IconURL string `json:"icon_url"`
|
||||
UnfurlLinks int `json:"unfurl_links"`
|
||||
LinkNames int `json:"link_names"`
|
||||
Attachments []SlackAttachments `json:"attachments"`
|
||||
}
|
||||
|
||||
type SlackAttachments struct {
|
||||
Fallback string `json:"fallback"`
|
||||
Color string `json:"color"`
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
Fallback string `json:"fallback"`
|
||||
Color string `json:"color"`
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
@@ -29,45 +29,47 @@ package slack
|
||||
// handler for it.
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
// stdlib
|
||||
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SlackAPIServer struct {}
|
||||
type APIServer struct{}
|
||||
|
||||
func (sh SlackAPIServer) Initialize() {
|
||||
c.Log.Infoln("Initializing Slack API handler...")
|
||||
func (sh APIServer) Initialize() {
|
||||
c.Log.Infoln("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()
|
||||
// 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()
|
||||
|
||||
httpsrv = &http.Server{
|
||||
Addr: cfg.SlackHandler.Listener.Address,
|
||||
// This handler will figure out from where request has come and will
|
||||
// send it to approriate pusher. Pusher should also determine to which
|
||||
// connection data should be sent.
|
||||
Handler: SlackHandler{},
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
httpsrv = &http.Server{
|
||||
Addr: cfg.SlackHandler.Listener.Address,
|
||||
// This handler will figure out from where request has come and will
|
||||
// send it to appropriate pusher. Pusher should also determine to which
|
||||
// connection data should be sent.
|
||||
Handler: Handler{},
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
go httpsrv.ListenAndServe()
|
||||
c.Log.Infof("Slack Webhooks API server starting to listen on %s", cfg.SlackHandler.Listener.Address)
|
||||
go func() {
|
||||
_ = httpsrv.ListenAndServe()
|
||||
}()
|
||||
|
||||
c.Log.Infof("Slack Webhooks API server starting to listen on %s", cfg.SlackHandler.Listener.Address)
|
||||
}
|
||||
|
||||
func (sh SlackAPIServer) handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Hello, %q", r.URL.Path)
|
||||
}
|
||||
func (sh APIServer) Shutdown() {
|
||||
c.Log.Infoln("Shutting down Slack API handler...")
|
||||
|
||||
func (sh SlackAPIServer) Shutdown() {
|
||||
c.Log.Infoln("Shutting down Slack API handler...")
|
||||
httpsrv.Shutdown(nil)
|
||||
c.Log.Infoln("Slack API HTTP server shutted down")
|
||||
_ = httpsrv.Shutdown(context.TODO())
|
||||
|
||||
c.Log.Infoln("Slack API HTTP server shutted down")
|
||||
}
|
||||
|
@@ -27,12 +27,12 @@ import (
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"gitlab.com/pztrn/opensaps/slack/message"
|
||||
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
|
||||
)
|
||||
|
||||
type SlackHandler struct{}
|
||||
type Handler struct{}
|
||||
|
||||
func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
|
||||
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)
|
||||
|
||||
// We should catch only POST requests. Otherwise return HTTP 404.
|
||||
@@ -40,6 +40,7 @@ func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Reque
|
||||
c.Log.Debugln("Not a POST request, returning HTTP 404")
|
||||
respwriter.WriteHeader(404)
|
||||
fmt.Fprintf(respwriter, "NOT FOUND")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,45 +50,51 @@ func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Reque
|
||||
c.Log.Debugf("Received body: %s", string(body))
|
||||
|
||||
// Try to figure out where we should push received data.
|
||||
url_splitted := strings.Split(req.URL.Path, "/")
|
||||
fmt.Println(url_splitted)
|
||||
cfg := c.Config.GetConfig()
|
||||
|
||||
var sent_to_pusher bool = false
|
||||
var sentToPusher bool = false
|
||||
|
||||
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'", name, config.Remote.PushTo, config.Remote.Pusher)
|
||||
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'",
|
||||
name, config.Remote.PushTo, config.Remote.Pusher)
|
||||
// Parse message into SlackMessage structure.
|
||||
if strings.Contains(string(body)[0:7], "payload") {
|
||||
// We have HTTP form payload. It still should be a
|
||||
// parseable JSON string, we just need to do some
|
||||
// preparations.
|
||||
// First - remove "payload=" from the beginning.
|
||||
temp_body := string(body)
|
||||
temp_body = strings.Replace(temp_body, "payload=", "", 1)
|
||||
tempBody := string(body)
|
||||
tempBody = strings.Replace(tempBody, "payload=", "", 1)
|
||||
// Second - unescape data.
|
||||
temp_body, err := url.QueryUnescape(temp_body)
|
||||
tempBody, err := url.QueryUnescape(tempBody)
|
||||
if err != nil {
|
||||
c.Log.Errorln("Failed to decode body into parseable string!")
|
||||
return
|
||||
}
|
||||
|
||||
// And finally - convert body back to bytes.
|
||||
body = []byte(temp_body)
|
||||
body = []byte(tempBody)
|
||||
}
|
||||
|
||||
slackmsg := slackmessage.SlackMessage{}
|
||||
|
||||
err := json.Unmarshal(body, &slackmsg)
|
||||
if err != nil {
|
||||
c.Log.Error("Failed to decode JSON into SlackMessage struct: '%s'", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.Log.Debug("Received message:", fmt.Sprintf("%+v", slackmsg))
|
||||
c.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg)
|
||||
sent_to_pusher = true
|
||||
|
||||
sentToPusher = true
|
||||
}
|
||||
}
|
||||
|
||||
if !sent_to_pusher {
|
||||
if !sentToPusher {
|
||||
c.Log.Debug("Don't know where to push data. Ignoring with HTTP 404")
|
||||
respwriter.WriteHeader(404)
|
||||
fmt.Fprintf(respwriter, "NOT FOUND")
|
||||
|
Reference in New Issue
Block a user