-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmain.go
55 lines (42 loc) · 1.1 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
package main
import (
"log"
"github.com/eko/pihole-exporter/config"
"github.com/eko/pihole-exporter/internal/metrics"
"github.com/eko/pihole-exporter/internal/pihole"
"github.com/eko/pihole-exporter/internal/server"
"github.com/xonvanetta/shutdown/pkg/shutdown"
)
func main() {
envConf, clientConfigs, err := config.Load()
if err != nil {
log.Fatal(err.Error())
}
metrics.Init()
serverDead := make(chan struct{})
clients := buildClients(clientConfigs, envConf)
s := server.NewServer(envConf.BindAddr, envConf.Port, clients)
go func() {
s.ListenAndServe()
close(serverDead)
}()
ctx := shutdown.Context()
go func() {
<-ctx.Done()
s.Stop()
}()
select {
case <-ctx.Done():
case <-serverDead:
}
log.Println("pihole-exporter HTTP server stopped")
}
func buildClients(clientConfigs []config.Config, envConfig *config.EnvConfig) []*pihole.Client {
clients := make([]*pihole.Client, 0, len(clientConfigs))
for i := range clientConfigs {
clientConfig := &clientConfigs[i]
client := pihole.NewClient(clientConfig, envConfig)
clients = append(clients, client)
}
return clients
}