-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (78 loc) · 2.39 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
package main
import (
"time"
"sync"
"github.com/breadinator/lost_ark_server_status/data"
"github.com/breadinator/lost_ark_server_status/scraper"
"github.com/breadinator/lost_ark_server_status/webhooks"
"github.com/kataras/golog"
mapset "github.com/deckarep/golang-set/v2"
)
const statusPageURL = "https://www.playlostark.com/en-us/support/server-status"
var config Config
var filterSet mapset.Set[string] = nil
var statuses = make(map[string]data.Server)
var statusesMutex sync.Mutex
var started bool = false
var lastOk bool = false
func main() {
golog.SetLevel(defaultLogLevel)
// Load config
golog.Info("Loading config...")
var err error
config, err = GetConfig()
if err != nil {
golog.Fatal(err)
return
}
golog.SetLevel(config.LogLevel)
golog.Debug(config)
if len(config.Filter) != 0 {
filterSet = mapset.NewSet(config.Filter...)
}
// Start looping
golog.Info("Starting loop...")
for {
go checkForChanges()
time.Sleep(config.WaitBetweenRequests)
}
}
func checkForChanges() error {
// Scrape for statuses
golog.Debug("Scraping page...")
serverStatuses, err := scraper.GetStatusFromURL(statusPageURL)
if err != nil {
golog.Error(err)
return err
}
// Update cached statuses
golog.Debug("Updating cache...")
diff := updateCache(serverStatuses)
// Push diff to webhook
var err2 error
if len(diff) != 0 {
golog.Info("Posting server status diff...")
err2 = webhooks.PostDiff(config.WebhookURL, diff)
if err2 != nil {
golog.Error(err2)
}
}
golog.Debug("Finished `checkForChanges`.")
return err2
}
func updateCache(serverStatuses map[string]data.Server) map[string][2]data.Status {
statusesMutex.Lock()
defer statusesMutex.Unlock()
diff := make(map[string][2]data.Status)
for key, newStatus := range serverStatuses {
if filterSet != nil && !filterSet.Contains(key) {
continue
}
oldStatus, contains := statuses[key]
if contains && oldStatus.ServerStatus != newStatus.ServerStatus {
diff[key] = [2]data.Status{oldStatus.ServerStatus, newStatus.ServerStatus}
}
statuses[key] = newStatus
}
return diff
}