Improvements when offline

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
2025-11-08 15:11:02 +00:00
parent 8086c7331a
commit f71b586186

17
main.go
View File

@@ -31,8 +31,13 @@ func init() {
func main() { func main() {
go (func() { go (func() {
for { for {
checkOnline() result, wait := checkOnline()
time.Sleep(time.Minute * time.Duration(interval)) if result {
isOnline = 1
} else {
isOnline = 0
}
time.Sleep(time.Minute * time.Duration(wait))
} }
})() })()
@@ -42,15 +47,17 @@ func main() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
} }
func checkOnline() { func checkOnline() (bool, int) {
client := http.Client{ client := http.Client{
Timeout: time.Duration(timeout) * time.Millisecond, Timeout: time.Duration(timeout) * time.Millisecond,
} }
_, err := client.Get(endpoint) _, err := client.Get(endpoint)
if err != nil { if err != nil {
isOnline = 0 fmt.Printf("Failed to access endpoint: %v\n", err)
// Re-check again after a minute when internet down
return false, 1
} else { } else {
isOnline = 1 return true, interval
} }
} }