-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
68 lines (55 loc) · 1.51 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
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/CSSUoB/society-voting/internal/config"
"github.com/CSSUoB/society-voting/internal/database"
"github.com/CSSUoB/society-voting/internal/discordWebhookNotify"
"github.com/CSSUoB/society-voting/internal/events"
"github.com/CSSUoB/society-voting/internal/httpcore"
)
var exitingForRestart = false
func main() {
if err := run(); err != nil {
slog.Error("Unhandled error", "error", err)
os.Exit(1)
}
if exitingForRestart {
os.Exit(153)
}
}
func run() error {
slog.Info("starting society-voting")
conf := config.Get()
if conf.Debug {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
})))
}
if err := database.Migrate(database.Get()); err != nil {
return fmt.Errorf("migrate dataase: %w", err)
}
if conf.Platform.DiscordWebhook.URL != "" {
go discordWebhookNotify.Run()
} else {
slog.Warn("discord webhook event notifier disabled")
}
httpcore.InitialiseSigner(conf.Platform.SessionSigningToken)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if os.Getenv("SOCIETY_VOTING_RESTART_ENABLED") != "" {
slog.Info("restart shim enabled")
_, electionEndReceiver := events.NewReceiver(events.TopicElectionEnded)
go func() {
<-electionEndReceiver
slog.Info("election ended - restarting")
exitingForRestart = true
cancel()
}()
}
return httpcore.ListenAndServe(ctx, conf.HTTP.Address())
}