-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgr.go
47 lines (43 loc) · 1.06 KB
/
msgr.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
package nopaste
import (
"log"
"net/http"
)
const MsgrRoot = "/irc-msgr"
func RunMsgr(configFile string) error {
var err error
config, err = LoadConfig(configFile)
if err != nil {
return err
}
var chs []MessageChan
if config.IRC != nil {
ircCh := make(IRCMessageChan, MsgBufferLen)
chs = append(chs, ircCh)
go RunIRCAgent(config, ircCh)
}
if config.Slack != nil {
slackCh := make(SlackMessageChan, MsgBufferLen)
chs = append(chs, slackCh)
go RunSlackAgent(config, slackCh)
}
http.HandleFunc(MsgrRoot+"/post", func(w http.ResponseWriter, req *http.Request) {
msgrPostHandler(w, req, chs)
})
log.Fatal(http.ListenAndServe(config.Listen, nil))
return nil
}
func msgrPostHandler(w http.ResponseWriter, req *http.Request, chs []MessageChan) {
channel := req.FormValue("channel")
msg := req.FormValue("msg")
if channel == "" || msg == "" || req.Method != "POST" {
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
return
}
for _, ch := range chs {
ch.PostMsgr(req)
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte{})
}