forked from resourced/resourced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (64 loc) · 2.02 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
package main
import (
"errors"
"net/http"
"os"
"runtime"
"github.com/Sirupsen/logrus"
"github.com/resourced/resourced/agent"
resourced_config "github.com/resourced/resourced/config"
_ "github.com/resourced/resourced/readers/docker"
_ "github.com/resourced/resourced/readers/haproxy"
_ "github.com/resourced/resourced/readers/mcrouter"
_ "github.com/resourced/resourced/readers/mysql"
_ "github.com/resourced/resourced/readers/procfs"
_ "github.com/resourced/resourced/readers/redis"
_ "github.com/resourced/resourced/readers/varnish"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
// main runs the web server for resourced.
func main() {
configDir := os.Getenv("RESOURCED_CONFIG_DIR")
if configDir == "" {
err := errors.New("RESOURCED_CONFIG_DIR is required")
logrus.Fatal(err)
}
// Create default configDir if necessary
if _, err := os.Stat(configDir); err != nil {
if os.IsNotExist(err) {
err := resourced_config.NewDefaultConfigs(configDir)
if err != nil {
logrus.Fatal(err)
}
}
}
a, err := agent.New()
if err != nil {
logrus.Fatal(err)
}
logLevel, err := logrus.ParseLevel(a.GeneralConfig.LogLevel)
if err == nil {
logrus.SetLevel(logLevel)
}
a.RunAllForever()
logFields := logrus.Fields{
"Addr": a.GeneralConfig.Addr,
"LogLevel": a.GeneralConfig.LogLevel,
"AllowedNetworks": a.GeneralConfig.AllowedNetworks,
"ResourcedMaster.URL": a.GeneralConfig.ResourcedMaster.URL,
}
if a.GeneralConfig.HTTPS.CertFile != "" && a.GeneralConfig.HTTPS.KeyFile != "" {
logFields["HTTPS.CertFile"] = a.GeneralConfig.HTTPS.CertFile
logFields["HTTPS.KeyFile"] = a.GeneralConfig.HTTPS.KeyFile
logrus.WithFields(logFields).Info("Running HTTPS server")
err = http.ListenAndServeTLS(a.GeneralConfig.Addr, a.GeneralConfig.HTTPS.CertFile, a.GeneralConfig.HTTPS.KeyFile, a.HttpRouter())
} else {
logrus.WithFields(logFields).Info("Running HTTP server")
err = http.ListenAndServe(a.GeneralConfig.Addr, a.HttpRouter())
}
if err != nil {
logrus.Fatal(err)
}
}