-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (112 loc) · 3.35 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
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
package main
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/simpledotorg/rtsl_exporter/alphasms"
"github.com/simpledotorg/rtsl_exporter/dhis2"
"github.com/simpledotorg/rtsl_exporter/sendgrid"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"time"
)
type Config struct {
ALPHASMSAPIKey string `yaml:"alphasms_api_key"`
SendGridAccounts []sendgrid.AccountConfig `yaml:"sendgrid_accounts"`
DHIS2Endpoints []struct {
BaseURL string `yaml:"base_url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"dhis2_endpoints"`
}
func readConfig(configPath string) (*Config, error) {
config := &Config{}
yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
return nil, err
}
return config, nil
}
func gracefulShutdown(server *http.Server) {
// Create a context with a timeout for the shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Notify when the shutdown process is complete
idleConnectionsClosed := make(chan struct{})
go func() {
defer close(idleConnectionsClosed)
if err := server.Shutdown(ctx); err != nil {
log.Printf("HTTP Server Shutdown Error: %v", err)
}
}()
// Wait for the server to shut down
select {
case <-ctx.Done():
log.Println("HTTP Server Shutdown Timeout")
case <-idleConnectionsClosed:
log.Println("HTTP Server Shutdown Complete")
}
}
func main() {
log.SetFlags(0)
config, err := readConfig("config.yaml")
if err != nil {
log.Fatalf("Error reading config file: %v", err)
}
// Alphasms
if config.ALPHASMSAPIKey == "" {
log.Fatalf("ALPHASMS_API_KEY not provided in config file")
}
alphasmsClient := alphasms.Client{APIKey: config.ALPHASMSAPIKey}
alphasmsExporter := alphasms.NewExporter(&alphasmsClient)
prometheus.MustRegister(alphasmsExporter)
// DHIS2
dhis2Clients := []*dhis2.Client{}
for _, endpoint := range config.DHIS2Endpoints {
dhis2Client := dhis2.Client{
Username: endpoint.Username,
Password: endpoint.Password,
BaseURL: endpoint.BaseURL,
ConnectionTimeout: dhis2.DefaultConnectionTimeout,
}
dhis2Clients = append(dhis2Clients, &dhis2Client)
}
dhis2Exporter := dhis2.NewExporter(dhis2Clients)
prometheus.MustRegister(dhis2Exporter)
// Register SendGrid exporters with time zones and handle subusers
sendGridConfigMap := make(map[string]sendgrid.AccountConfig)
for _, account := range config.SendGridAccounts {
sendGridConfigMap[account.AccountName] = sendgrid.AccountConfig{
AccountName: account.AccountName,
APIKey: account.APIKey,
TimeZone: account.TimeZone,
Subusers: account.Subusers,
}
}
sendgridExporter := sendgrid.NewExporter(sendGridConfigMap)
prometheus.MustRegister(sendgridExporter)
http.Handle("/metrics", promhttp.Handler())
log.Println("Starting server on :8080")
httpServer := &http.Server{
Addr: ":8080",
}
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
log.Println("Shutdown signal received")
gracefulShutdown(httpServer)
}()
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe Error: %v", err)
}
log.Println("Bye bye")
}