-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpropanebot.go
More file actions
89 lines (77 loc) · 1.92 KB
/
propanebot.go
File metadata and controls
89 lines (77 loc) · 1.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"encoding/json"
"log"
"os"
"os/signal"
"syscall"
"golang.org/x/sync/errgroup"
)
// func getenv(name string) string {
// v := os.Getenv(name)
// if v == "" {
// panic("missing required environment variable " + name)
// }
// return v
// }
type AppConfig struct {
MQTT struct {
Server string `json:"server"`
Topic string `json:"topic"`
} `json:"mqtt"`
Discord struct {
AppToken string `json:"appToken"`
GuildID string `json:"guildId"`
BotToken string `json:"botToken"`
} `json:"discord"`
Slack struct {
APIToken string `json:"apiToken"`
} `json:"slack"`
}
func main() {
// Let's begin by reading the cylinder settings
LoadCylinderData()
ds := NewDatastore()
var cfg AppConfig
if err := LoadConfig("./config.json", &cfg); err != nil {
panic("Failed to load config: " + err.Error())
}
// Get a Context that can handle stopping for signals, timeouts, or whatever else we throw at it
ctx, done := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer done()
wg, ctx := errgroup.WithContext(ctx)
// Now start the mqtt stuff so we can start getting messages
wg.Go((&MQTTListener{
Datastore: ds,
Server: cfg.MQTT.Server,
Topic: cfg.MQTT.Topic,
}).Run(ctx))
// Setup and run Discord
wg.Go((&DiscordBot{
AppToken: cfg.Discord.AppToken,
GuildID: cfg.Discord.GuildID,
BotToken: cfg.Discord.BotToken,
Datastore: ds,
}).Run(ctx))
// Now begins the Slack stuff
wg.Go((&SlackBot{
APIToken: cfg.Slack.APIToken,
Datastore: ds,
}).Run(ctx))
// Start the web server on port 9991
wg.Go((&WebServer{
Port: 9991,
Datastore: ds,
}).Run(ctx))
// Wait for exit and print any error messages that bubble up
log.Printf("Exiting with message: %q\n", wg.Wait())
}
func LoadConfig(path string, cfg *AppConfig) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return json.NewDecoder(f).Decode(cfg)
}