-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (101 loc) · 2.55 KB
/
Copy pathmain.go
File metadata and controls
134 lines (101 loc) · 2.55 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-co-op/gocron"
"region-exporter/config"
"region-exporter/metrics"
"region-exporter/models"
netutil "region-exporter/net"
"region-exporter/services"
"region-exporter/web"
)
var (
version = "unknown"
)
type App struct {
client *http.Client
services []services.Service
abuseIPDBChecker *services.AbuseIPDBChecker
serverIP string
expectedRegion string
servicesData []models.ServiceStatus
}
func NewApp() *App {
cfg := &config.CLIConfig
transport, err := netutil.CreateTransport(cfg)
if err != nil {
log.Fatalf("Failed to create HTTP transport: %v", err)
}
client := &http.Client{
Timeout: time.Duration(cfg.Check.Timeout) * time.Second,
Transport: transport,
}
app := &App{
client: client,
abuseIPDBChecker: services.NewAbuseIPDBChecker(client),
}
app.services = services.GetAllServices(client)
return app
}
func (a *App) runChecks() {
cfg := &config.CLIConfig
log.Println("Starting region checks...")
a.servicesData = []models.ServiceStatus{}
for _, svc := range a.services {
status := services.CheckService(svc, cfg, a.serverIP, a.expectedRegion)
if status.Enabled {
a.servicesData = append(a.servicesData, status)
}
}
a.abuseIPDBChecker.Check(a.serverIP)
web.UpdateData(a.serverIP, a.expectedRegion, a.servicesData)
log.Println("Checks completed")
}
func (a *App) startScheduler(ctx context.Context) {
cfg := &config.CLIConfig
s := gocron.NewScheduler(time.UTC)
interval := time.Duration(cfg.Check.Interval) * time.Second
_, err := s.Every(interval).StartImmediately().Do(a.runChecks)
if err != nil {
log.Fatalf("Failed to schedule checks: %v", err)
}
log.Printf("Scheduler started with interval: %s", interval)
s.StartAsync()
<-ctx.Done()
s.Stop()
log.Println("Scheduler stopped")
}
func (a *App) Run() error {
cfg := &config.CLIConfig
serverIP, expectedRegion, err := netutil.DetectServerInfo(a.client, cfg)
if err != nil {
return err
}
a.serverIP = serverIP
a.expectedRegion = expectedRegion
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("Received shutdown signal")
cancel()
}()
go a.startScheduler(ctx)
return web.StartHTTPServer(ctx, cfg)
}
func main() {
config.Parse(version)
metrics.Init()
app := NewApp()
if err := app.Run(); err != nil {
log.Fatalf("Application error: %v", err)
}
}