-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
64 lines (56 loc) · 1.15 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/nsqio/go-nsq"
"github.com/nsqio/nsqadmin_to_slack/internal/slack"
)
type Handler struct {
Slack *slack.Slack
SlackChannel string
}
type Action struct {
Action string `json:"action"`
}
func (h *Handler) HandleMessage(m *nsq.Message) error {
var a Action
err := json.Unmarshal(m.Body, &a)
if err != nil {
return err
}
type handler struct {
Action string
Handler func(b []byte) error
}
handlers := []handler{
{"tombstone_topic_producer", h.nsqadmin},
{"create_topic", h.nsqadmin},
{"create_channel", h.nsqadmin},
{"delete_topic", h.nsqadmin},
{"delete_channel", h.nsqadmin},
{"pause_channel", h.nsqadmin},
{"pause_topic", h.nsqadmin},
{"unpause_channel", h.nsqadmin},
{"unpause_topic", h.nsqadmin},
{"empty_channel", h.nsqadmin},
{"empty_topic", h.nsqadmin},
}
var errors bool
for _, hh := range handlers {
switch {
case hh.Action == a.Action:
fallthrough
case hh.Action == "":
err = hh.Handler(m.Body)
if err != nil {
log.Printf("%s", err)
errors = true
}
}
}
if errors {
return fmt.Errorf("failed handling")
}
return nil
}