/
promulgator.go
73 lines (58 loc) · 1.86 KB
/
promulgator.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
65
66
67
68
69
70
71
72
73
package main
import (
"net/http"
"os"
"strings"
"sync"
"github.com/kelseyhightower/envconfig"
"github.com/spacemonkeygo/spacelog"
"github.com/dimfeld/promulgator/commandrouter"
"github.com/dimfeld/promulgator/jiraclient"
"github.com/dimfeld/promulgator/jirawebhook"
"github.com/dimfeld/promulgator/model"
"github.com/dimfeld/promulgator/slackclient"
)
func readConfig() (c *model.Config, err error) {
c = new(model.Config)
err = envconfig.Process("PROMULGATOR", c)
if c.SlackSlashCommandKey == "" {
c.SlackSlashCommandKey = c.SlackKey
}
if !strings.HasSuffix(c.JiraUrl, "/") {
c.JiraUrl = c.JiraUrl + "/"
}
return
}
func main() {
var config *model.Config
var err error
spacelog.MustSetup("promulgator", spacelog.SetupConfig{Output: "stderr", Stdlevel: "warn"})
mainLogger := spacelog.GetLoggerNamed("main")
if config, err = readConfig(); err != nil {
mainLogger.Critf("Error reading config: %s\n", err.Error())
os.Exit(1)
}
// Channel for sending messages out to Slack.
slackOutChan := make(chan *model.ChatMessage, 1)
// This channel is closed when the server is done. At present, there is no
// reason to ever do this, but the option is here.
closeChan := make(chan struct{})
wg := &sync.WaitGroup{}
// Route incoming messages from Slack to the appropriate destination.
router := commandrouter.New()
jiraclient.Start(config, wg, router, slackOutChan, closeChan)
jirawebhook.Start(config, wg, slackOutChan, closeChan)
slackclient.Start(config, wg, slackOutChan, router, closeChan)
go func() {
// TODO Support TLS. Unimportant for now only because this runs solely
// within our own network, using nginx for TLS termination.
mainLogger.Noticef("Listening on %s", config.WebHookBind)
err := http.ListenAndServe(config.WebHookBind, nil)
if err != nil {
mainLogger.Crit(err.Error())
os.Exit(1)
}
}()
wg.Wait()
router.Close()
}