2019-05-08 23:45:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-26 23:36:30 +01:00
|
|
|
"log"
|
2019-05-08 23:45:04 +02:00
|
|
|
|
|
|
|
"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"
|
2021-08-16 23:55:05 +02:00
|
|
|
"github.com/xonvanetta/shutdown/pkg/shutdown"
|
2019-05-08 23:45:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-12-26 23:36:30 +01:00
|
|
|
envConf, clientConfigs, err := config.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
|
|
|
|
metrics.Init()
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
serverDead := make(chan struct{})
|
2021-12-10 15:39:22 +01:00
|
|
|
|
|
|
|
clients := buildClients(clientConfigs)
|
2021-12-10 04:48:28 +01:00
|
|
|
|
|
|
|
s := server.NewServer(envConf.Port, clients)
|
2021-08-16 23:55:05 +02:00
|
|
|
go func() {
|
|
|
|
s.ListenAndServe()
|
|
|
|
close(serverDead)
|
|
|
|
}()
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
ctx := shutdown.Context()
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
s.Stop()
|
|
|
|
}()
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-serverDead:
|
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
fmt.Println("pihole-exporter HTTP server stopped")
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
2021-12-10 15:39:22 +01:00
|
|
|
|
|
|
|
func buildClients(clientConfigs []config.Config) []*pihole.Client {
|
|
|
|
clients := make([]*pihole.Client, 0, len(clientConfigs))
|
|
|
|
for i := range clientConfigs {
|
|
|
|
clientConfig := &clientConfigs[i]
|
|
|
|
|
|
|
|
client := pihole.NewClient(clientConfig)
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
return clients
|
|
|
|
}
|