make lint happy
This commit is contained in:
parent
2cf73b07be
commit
64b2bb4f78
|
@ -16,20 +16,17 @@
|
|||
|
||||
package webhooks
|
||||
|
||||
|
||||
import (
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
|
||||
func GenerateMiddleware() message.HandlerMiddleware {
|
||||
runtimeConfig := ProcessConfig()
|
||||
return func(hf message.HandlerFunc) message.HandlerFunc {
|
||||
return func(msg *message.Message) ([]*message.Message, error) {
|
||||
topic := message.SubscribeTopicFromCtx(msg.Context() )
|
||||
topic := message.SubscribeTopicFromCtx(msg.Context())
|
||||
handleOutgoingWebhook(runtimeConfig, topic, msg)
|
||||
newMsg, err := hf(msg)
|
||||
if err != nil {
|
||||
|
@ -40,8 +37,6 @@ func GenerateMiddleware() message.HandlerMiddleware {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func handleOutgoingWebhook(cfg []WebhookRuntimeConfig, topic string, msg *message.Message) {
|
||||
log.Debugf("Webhook handler for '%s'", topic)
|
||||
for i, entry := range cfg {
|
||||
|
@ -52,4 +47,3 @@ func handleOutgoingWebhook(cfg []WebhookRuntimeConfig, topic string, msg *messag
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,61 +17,56 @@
|
|||
package webhooks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 5 * time.Second
|
||||
ctHeader = "Content-Type"
|
||||
ctValue = "application/json"
|
||||
hmacHeader = "X-Signature"
|
||||
ctHeader = "Content-Type"
|
||||
ctValue = "application/json"
|
||||
hmacHeader = "X-Signature"
|
||||
)
|
||||
|
||||
type FilteringFunction func(string) (bool)
|
||||
type WebhookCallFunction func(string, *message.Message) (error)
|
||||
|
||||
type FilteringFunction func(string) bool
|
||||
type WebhookCallFunction func(string, *message.Message) error
|
||||
|
||||
// Single configuration entry
|
||||
type SingleConfEntry struct {
|
||||
Events []string `json:"events"`
|
||||
URL string `json:"url"`
|
||||
Secret string `json:"secret"`
|
||||
Timeout int `json:"timeout"`
|
||||
Events []string `json:"events"`
|
||||
URL string `json:"url"`
|
||||
Secret string `json:"secret"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
|
||||
type WebhookRuntimeConfig struct {
|
||||
FilterFunc FilteringFunction
|
||||
ExecuteFunc WebhookCallFunction
|
||||
FilterFunc FilteringFunction
|
||||
ExecuteFunc WebhookCallFunction
|
||||
}
|
||||
|
||||
|
||||
|
||||
func getWebhookFilterFunc(cfg SingleConfEntry) FilteringFunction {
|
||||
return func(topic string) (is_interesting bool) {
|
||||
for _, filter := range cfg.Events {
|
||||
log.Debugf("Match pattern:'%s' agains:'%s'", filter, topic)
|
||||
log.Debugf("Match pattern:'%s' topic:'%s'", filter, topic)
|
||||
if filter == "*" {
|
||||
log.Debugf(" '*' == Always match ")
|
||||
return true
|
||||
} else {
|
||||
if strings.HasPrefix(topic, filter) {
|
||||
log.Debugf("Positive match [%s] -> [%s]", filter, topic)
|
||||
return true
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(topic, filter) {
|
||||
log.Debugf("Positive match [%s] -> [%s]", filter, topic)
|
||||
return true
|
||||
}
|
||||
}
|
||||
log.Debugf("No match for [%s]", topic)
|
||||
|
@ -80,21 +75,21 @@ func getWebhookFilterFunc(cfg SingleConfEntry) FilteringFunction {
|
|||
}
|
||||
|
||||
func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
||||
return func(topic string, msg *message.Message) (error) {
|
||||
return func(topic string, msg *message.Message) error {
|
||||
endpointURL := cfg.URL
|
||||
hmacKey := cfg.Secret
|
||||
timeout := defaultTimeout
|
||||
if cfg.Timeout > 0 {
|
||||
timeout = time.Second * time.Duration(cfg.Timeout)
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("Webhook Call : %s (key=%s)", endpointURL, hmacKey)
|
||||
webhookUrl := fmt.Sprintf("%s%s", endpointURL, topic)
|
||||
|
||||
webhookURL := fmt.Sprintf("%s%s", endpointURL, topic)
|
||||
|
||||
rawData := msg.Payload
|
||||
|
||||
req, err := http.NewRequest( http.MethodPost, webhookUrl, bytes.NewBuffer(rawData))
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(rawData))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -102,7 +97,7 @@ func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
|||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
|
||||
|
||||
req.Header.Set(ctHeader, ctValue)
|
||||
|
||||
if len(hmacKey) > 1 {
|
||||
|
@ -110,31 +105,27 @@ func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
|||
req.Header.Set(hmacHeader, signature)
|
||||
}
|
||||
|
||||
_, err = client.Do(req)
|
||||
|
||||
resp, err = client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Debugf("Webhook failed : %s , +%v", webhookUrl , err)
|
||||
log.Debugf("Webhook failed : %s , +%v", webhookURL, err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("Webhook success : %s ", webhookUrl )
|
||||
log.Debugf("Webhook success : %s ", webhookURL)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func GenerateHMAC(data []byte, key string) string {
|
||||
h := hmac.New(sha256.New, []byte(key))
|
||||
h.Write(data)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Process config and prepare mapping
|
||||
func ProcessConfig() ([]WebhookRuntimeConfig) {
|
||||
|
||||
func ProcessConfig() []WebhookRuntimeConfig {
|
||||
|
||||
var items []SingleConfEntry
|
||||
|
||||
config.WebhooksConf.GetUnmarshaled(&items)
|
||||
|
@ -144,7 +135,7 @@ func ProcessConfig() ([]WebhookRuntimeConfig) {
|
|||
log.Debugf("Webhook config items : %+v\n", items)
|
||||
|
||||
for i, item := range items {
|
||||
runtime[i].FilterFunc = getWebhookFilterFunc(item)
|
||||
runtime[i].FilterFunc = getWebhookFilterFunc(item)
|
||||
runtime[i].ExecuteFunc = getWebhookCallFunc(item)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue