-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
109 lines (98 loc) · 2.71 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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/integr8ly/workload-web-app/pkg/checks"
"github.com/integr8ly/workload-web-app/pkg/counters"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
const (
evnVarPort = "PORT"
envVarEnvironment = "ENVIRONMENT"
envVarRequestInterval = "REQUEST_INTERVAL"
envVarURL = "RHSSO_SERVER_URL"
envVarUser = "RHSSO_USER"
envVarPassword = "RHSSO_PWD"
envVarThreeScaleURL = "THREE_SCALE_URL"
productionEnv = "production"
)
func init() {
log.SetOutput(os.Stdout)
if strings.ToLower(os.Getenv(envVarEnvironment)) == productionEnv {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.DebugLevel)
}
if os.Getenv(envVarRequestInterval) != "" {
if val, err := strconv.Atoi(os.Getenv(envVarRequestInterval)); err == nil {
counters.RequestInterval = time.Duration(val) * time.Second
}
}
prometheus.MustRegister(counters.ServiceUpGauge)
prometheus.MustRegister(counters.ServiceTotalRequestsCounter)
prometheus.MustRegister(counters.ServiceTotalErrorsCounter)
prometheus.MustRegister(counters.ServiceTotalDowntimeCounter)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
func startSSOChecks() {
url := os.Getenv(envVarURL)
user := os.Getenv(envVarUser)
pwd := os.Getenv(envVarPassword)
realm := "master"
if url != "" && user != "" && pwd != "" && realm != "" {
log.WithFields(log.Fields{
"serverURL": url,
"realmName": realm,
"interval": counters.RequestInterval,
}).Info("Start SSO Checks")
c := &checks.SSOChecks{
ServerURL: url,
User: user,
Password: pwd,
RealmName: realm,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("SSO checks are not started as env vars are not set correctly!")
}
}
func startThreeScaleChecks() {
url := os.Getenv(envVarThreeScaleURL)
if url != "" {
log.WithFields(log.Fields{
"url": url,
"interval": counters.RequestInterval,
}).Info("Start 3scale checks")
c := &checks.ThreeScaleChecks{
URL: url,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("3sclae: 3scale checks are not started because the environment variables %s is not set", envVarThreeScaleURL)
}
}
func main() {
go startSSOChecks()
go startThreeScaleChecks()
startHttpServer()
}
func startHttpServer() {
p := os.Getenv(evnVarPort)
if p == "" {
p = "8080"
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", handler)
log.WithField("port", p).Infof("Starting HTTP server")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", p), nil))
}