This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
@@ -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")
|
||||
}
|
||||
|
@@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user