-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
116 lines (92 loc) · 2.82 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/alexliesenfeld/health"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
/*
Notes about development:
- All functions that use an topic and message should have the topic passed as
immediately before the message. This makes it easier to reason about
not having to remember which is which.
- Try to use handleError() as much as possible to reduce the amount of
boilerplate code.
*/
func main() {
var (
topic = "discord.inbound.*"
)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Info().Msg("Hello, world!")
rdb := redisConnect(os.Getenv("REDIS_URL"), context.Background())
// Create a new pubsub client that listens to the topic from BYTEBOT_TOPIC or "discord.inbound.*"
log.Info().Msg("Creating new pubsub client")
if os.Getenv("BYTEBOT_TOPIC") != "" {
topic = os.Getenv("BYTEBOT_TOPIC")
} else {
log.Info().Msg("BYTEBOT_TOPIC not set, using default topic: " + topic)
}
pubsub := rdb.PSubscribe(context.Background(), topic)
defer pubsub.Close()
log.Info().Msgf("Subscribed to topic: %s", topic)
ch := pubsub.Channel()
// Set a shared context for all lambdas
ctx := context.Background()
var lambdas = []lambda{
weatherLambda,
reactionsLambda,
}
// Start a goroutine that will listen for messages
go func() {
for {
// Read message from channel
msg := <-ch
// Unmarshal the message into a discordgo.Message
dgoMessage, err := unmarshalDiscordMessage(msg.Payload)
if err != nil {
log.Error().Err(err).Msg("Error unmarshalling message")
}
// Convert the topic string into a pubsubDiscordTopicAddr struct
topicAddr, err := newPubsubDiscordTopicAddr(msg.Channel)
if err != nil {
log.Error().
Err(err).
Str("topic", msg.Channel).
Msg("Error converting topic string to pubsubDiscordTopicAddr struct")
}
for _, lambda := range lambdas {
go lambda(ctx, rdb, topicAddr, dgoMessage)
}
}
}()
// Add a healthcheck endpoint on port 8080
log.Info().Msg("Registering healthcheck endpoint")
http.Handle("/health", health.NewHandler(newHealthChecker()))
log.Info().Msg("Starting http server on port 8080")
http.ListenAndServe(":8080", nil)
}
func newHealthChecker() health.Checker {
return health.NewChecker(
health.WithCacheDuration(1*time.Second),
health.WithTimeout(10*time.Second),
health.WithCheck(
health.Check{
Name: "redis",
Timeout: 2 * time.Second,
Check: func(ctx context.Context) error {
log.Info().Msg("Running redis check")
return nil
},
},
),
// Set a status listener that will be invoked when the health status changes.
// More powerful hooks are also available (see docs).
health.WithStatusListener(func(ctx context.Context, state health.CheckerState) {
log.Info().Msgf("Health status changed: %s", state.Status)
}),
)
}