-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
93 lines (80 loc) · 2.1 KB
/
config.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
package config
import (
"net/http"
"os"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/lager"
cfclient "github.com/cloudfoundry-community/go-cfclient"
)
type Config struct {
DeployEnv string
AWSRegion string
Logger lager.Logger
ListenPort uint
CFClientConfig *cfclient.Config
ServiceName string
ServicePlanUpdateSchedule time.Duration
}
func NewConfigFromEnv(defaultServiceName string) Config {
return Config{
DeployEnv: GetEnvWithDefaultString("DEPLOY_ENV", "dev"),
AWSRegion: os.Getenv("AWS_REGION"),
Logger: getDefaultLogger(),
ListenPort: GetEnvWithDefaultInt("PORT", 9299),
CFClientConfig: &cfclient.Config{
ApiAddress: os.Getenv("CF_API_ADDRESS"),
Username: os.Getenv("CF_USERNAME"),
Password: os.Getenv("CF_PASSWORD"),
ClientID: os.Getenv("CF_CLIENT_ID"),
ClientSecret: os.Getenv("CF_CLIENT_SECRET"),
SkipSslValidation: os.Getenv("CF_SKIP_SSL_VALIDATION") == "true",
Token: os.Getenv("CF_TOKEN"),
UserAgent: os.Getenv("CF_USER_AGENT"),
HttpClient: &http.Client{
Timeout: 30 * time.Second,
},
},
ServiceName: GetEnvWithDefaultString("SERVICE_NAME", defaultServiceName),
ServicePlanUpdateSchedule: GetEnvWithDefaultDuration("SERVICE_PLAN_UPDATE_SCHEDULE", 15*time.Minute),
}
}
func GetEnvWithDefaultDuration(k string, def time.Duration) time.Duration {
v := GetEnvWithDefaultString(k, "")
if v == "" {
return def
}
d, err := time.ParseDuration(v)
if err != nil {
panic(err)
}
return d
}
func GetEnvWithDefaultString(k string, def string) string {
v := os.Getenv(k)
if v == "" {
return def
}
return v
}
func GetEnvWithDefaultInt(k string, def uint) uint {
v := os.Getenv(k)
if v == "" {
return def
}
d, err := strconv.ParseUint(v, 10, 32)
if err != nil {
panic(err)
}
return uint(d)
}
func getDefaultLogger() lager.Logger {
logger := lager.NewLogger("prometheus-endpoint")
logLevel := lager.INFO
if strings.ToLower(os.Getenv("LOG_LEVEL")) == "debug" {
logLevel = lager.DEBUG
}
logger.RegisterSink(lager.NewWriterSink(os.Stdout, logLevel))
return logger
}