-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
76 lines (60 loc) · 1.78 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
71
72
73
74
75
76
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/asaskevich/EventBus"
"github.com/bcneng/candebot/handlers"
"github.com/slack-go/slack/slackevents"
"github.com/bcneng/candebot/bot"
)
// Version is the bot version. Usually the git commit hash. Passed during building.
var Version = "unknown"
type initConfig struct {
ConfigFilePath string `env:"CONFIG_FILE_PATH"`
EnvVarsPrefix string `env:"ENV_VARS_PREFIX"`
}
var initConf = initConfig{}
func init() {
flag.StringVar(&initConf.ConfigFilePath, "config", "./.bot.toml", "path to config file (TOML)")
flag.StringVar(&initConf.EnvVarsPrefix, "env-prefix", "BOT_", "path to config file (TOML)")
flag.Parse()
}
func main() {
var conf bot.Config
conf.Version = Version
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// TODO Prefix and filepath from argument
err := bot.LoadConfigFromFileAndEnvVars(ctx, initConf.EnvVarsPrefix, initConf.ConfigFilePath, &conf)
if err != nil {
log.Fatal(err)
}
bus := EventBus.New()
subscribe(bus, slackevents.Message, handlers.MessageEventHandler)
subscribe(bus, slackevents.AppMention, handlers.AppMentionEventHandler)
ensureInterruptionsGracefullyShutdown(cancel)
if err := bot.WakeUp(ctx, conf, bus); err != nil && err != context.Canceled {
log.Fatal(err)
}
}
func subscribe(bus EventBus.Bus, t slackevents.EventsAPIType, h bot.EventHandler) {
if err := bus.Subscribe(string(t), bot.CreateEventHandler(t, h)); err != nil {
log.Fatal(err)
}
}
func ensureInterruptionsGracefullyShutdown(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-c
log.Println("Shutting down the app")
cancel()
time.Sleep(time.Second)
os.Exit(0)
}()
}