-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.go
54 lines (50 loc) · 1.21 KB
/
notify.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
package nsqadmin
import (
"encoding/base64"
"net/http"
"strings"
"time"
)
type AdminAction struct {
Action string `json:"action"`
Topic string `json:"topic"`
Channel string `json:"channel,omitempty"`
Node string `json:"node,omitempty"`
Timestamp int64 `json:"timestamp"`
User string `json:"user,omitempty"`
RemoteIP string `json:"remote_ip"`
UserAgent string `json:"user_agent"`
}
func basicAuthUser(req *http.Request) string {
s := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
return ""
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return ""
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return ""
}
return pair[0]
}
func (s *httpServer) notifyAdminAction(actionType string, topicName string,
channelName string, node string, req *http.Request) {
if s.ctx.nsqadmin.opts.NotificationHTTPEndpoint == "" {
return
}
action := &AdminAction{
actionType,
topicName,
channelName,
node,
time.Now().Unix(),
basicAuthUser(req),
req.RemoteAddr,
req.UserAgent(),
}
// Perform all work in a new goroutine so this never blocks
go func() { s.ctx.nsqadmin.notifications <- action }()
}