-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
70 lines (58 loc) · 1.48 KB
/
main.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
package main
import (
"fmt"
"net/http"
"os"
"time"
bugsnag "github.com/bugsnag/bugsnag-go"
"github.com/geckoboard/cake-bot/log"
"github.com/geckoboard/cake-bot/slack"
"github.com/joho/godotenv"
slackapi "github.com/slack-go/slack"
)
var (
logger log.LeveledLogger = log.New()
)
func main() {
if err := godotenv.Load(); err != nil {
logger.Error("msg", "Error loading .env file")
}
var (
httpPort = mustGetenv("PORT")
githubSecret = mustGetenv("GITHUB_SECRET")
slackToken = mustGetenv("SLACK_TOKEN")
)
slackClient := slack.New(slackToken)
go func() {
refreshSlackUsers(slackClient)
for range time.Tick(5 * time.Minute) {
refreshSlackUsers(slackClient)
}
}()
notifier := NewSlackNotifier(slackapi.New(slackToken))
webhookValidator := NewGitHubWebhookValidator(githubSecret)
httpServer := http.Server{
Addr: ":" + httpPort,
Handler: bugsnag.Handler(NewServer(notifier, webhookValidator)),
}
logger.Info("msg", fmt.Sprintf("Listening on port %s", httpPort))
_ = httpServer.ListenAndServe()
}
func refreshSlackUsers(slackClient *slack.Client) {
if err := slack.Users.Load(slackClient); err != nil {
logger.Error("msg", "couldn't load Slack users", "err", err)
}
}
func mustGetenv(key string) string {
str := os.Getenv(key)
if str == "" {
logger.Error("msg", fmt.Sprintf("Missing environment variable: %s", key))
os.Exit(1)
}
return str
}
func init() {
bugsnag.Configure(bugsnag.Configuration{
APIKey: os.Getenv("BUGSNAG_API_KEY"),
})
}