-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
95 lines (80 loc) · 2.44 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
package main
import (
goflag "flag"
"io/ioutil"
"log"
"github.com/facebookgo/pidfile"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
configPath string
)
// HTTPConfig contains necessary config parameters for a TLS or plain HTTP server
type HTTPConfig struct {
port int
certFile string
keyFile string
caCertFile string
}
func (c *HTTPConfig) isSecure() bool {
return c.certFile != "" && c.keyFile != ""
}
func init() {
flag.StringVar(&configPath, "config-path", "", "Absolute path to read a config file from")
flag.StringSlice("address", []string{"http://localhost:8080"}, "Server address and port to ping from the client")
flag.Int("port", 8080, "Server port to listen on")
flag.Int("interval", 10, "Interval in seconds for clients calling '/ping'")
flag.Float64("error-rate", 0.0, "Server error-rate in percent. Server will respond with an error instead of a regular 'pong' answer")
}
func main() {
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
flag.Parse()
err := viper.BindPFlags(flag.CommandLine)
if err != nil {
log.Fatalf("fatal error during binding flags: '%v'", err)
}
viper.AutomaticEnv()
readConfiguration()
writePIDFile(viper.GetString("pidfile"))
serverConfig := &HTTPConfig{
viper.GetInt("port"),
viper.GetString("cert-path"),
viper.GetString("key-path"),
viper.GetString("ca-cert-path"),
}
verifyHTTPSConfig(serverConfig)
pingServer := NewPingServer(serverConfig, viper.GetFloat64("error-rate"), viper.GetString("CF_INSTANCE_GUID"))
go pingServer.Start()
pingClient := NewPingClient(serverConfig, viper.GetStringSlice("address"), viper.GetDuration("interval"))
go pingClient.Start()
select {} // keep main running, as we only have gofuncs above
}
func readConfiguration() {
if configPath != "" {
viper.SetConfigFile(configPath)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("fatal error config file at '%s': %s", configPath, err)
}
content, _ := ioutil.ReadFile(configPath)
log.Printf("config file:'%s'\n", content)
}
}
func writePIDFile(path string) {
if path != "" {
pidfile.SetPidfilePath(path)
err := pidfile.Write()
if err != nil {
log.Fatalf("couldn't write Pidfile at path '%s': %s", path, err)
}
}
}
func verifyHTTPSConfig(sc *HTTPConfig) {
if sc.certFile != "" && sc.keyFile == "" {
log.Fatal("specify either certificate and key or none")
}
if sc.certFile == "" && sc.keyFile != "" {
log.Fatal("specify either certificate and key or none")
}
}