-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
78 lines (63 loc) · 1.64 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
package main
import (
"errors"
"io"
"log"
"net/http"
"time"
)
var (
ErrInvalidStatusCode error = errors.New("did not get an acceptiable status code from the server")
ErrFailedToDecodeBody error = errors.New("unable to decode the body")
ErrFailedToDecodeJson error = errors.New("unexpected json format was returned")
ErrWasNotJson error = errors.New("response from server was not json")
ErrDomainNotFound error = errors.New("unable to find requested domain on cloudflare")
)
func GetCurrentIpAddress() (string, error) {
resp, err := http.Get("https://v4.ident.me")
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", ErrInvalidStatusCode
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", ErrFailedToDecodeBody
}
return string(body), nil
}
func main() {
config := NewConfigClient()
cfg := config.LoadConfig()
if cfg.Email == "" {
log.Println("Unable to find 'EMAIL' env value.")
return
}
if cfg.Token == "" {
log.Println("Unable to find 'API_TOKEN' env value.")
return
}
if cfg.Domain == "" {
log.Println("Unable to find 'DOMAIN' env value.")
return
}
if len(cfg.Hosts) == 0 {
log.Println("Unable to find 'HOSTS' env value.")
}
log.Println("Config Check: OK")
cron := NewCron()
log.Println("Cloudflare Check will run every 15 minutes.")
cron.scheduler.AddFunc("0/5 * * * *", func() {
cron.RunCloudflareCheck(cfg.Token, cfg.Email, cfg.Domain, cfg.Hosts)
})
cron.scheduler.AddFunc("0/1 * * * *", func() {
cron.HelloWorldJob()
})
cron.scheduler.Start()
log.Println("Application has started!")
for {
time.Sleep(30 * time.Second)
}
}