/
main_config.go
165 lines (149 loc) · 4.19 KB
/
main_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/alphagov/paas-billing/cfstore"
"github.com/alphagov/paas-billing/eventcollector"
"github.com/alphagov/paas-billing/eventfetchers/cffetcher"
"github.com/alphagov/paas-billing/eventio"
cfclient "github.com/cloudfoundry-community/go-cfclient"
"github.com/pkg/errors"
"code.cloudfoundry.org/lager"
)
type Config struct {
AppRootDir string
Logger lager.Logger
Store eventio.EventStore
DatabaseURL string
Collector eventcollector.Config
CFFetcher cffetcher.Config
ServerPort int
Processor ProcessorConfig
HistoricDataCollector cfstore.Config
}
func (cfg Config) ConfigFile() (string, error) {
root := cfg.AppRootDir
p := filepath.Join(root, "config.json")
if _, err := os.Stat(p); os.IsNotExist(err) {
return "", fmt.Errorf("%s does not exist", p)
}
return p, nil
}
type ProcessorConfig struct {
Schedule time.Duration
}
func NewConfigFromEnv() (cfg Config, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("%v", r))
}
}()
rootDir := os.Getenv("APP_ROOT")
if rootDir == "" {
rootDir = getwd()
}
cfg = Config{
AppRootDir: rootDir,
Logger: lager.NewLogger("default"),
DatabaseURL: getEnvWithDefaultString("DATABASE_URL", "postgres://postgres:@localhost:5432/"),
HistoricDataCollector: cfstore.Config{
ClientConfig: &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,
},
},
Schedule: getEnvWithDefaultDuration("COLLECTOR_SCHEDULE", 15*time.Minute),
},
Collector: eventcollector.Config{
Schedule: getEnvWithDefaultDuration("COLLECTOR_SCHEDULE", 15*time.Minute),
MinWaitTime: getEnvWithDefaultDuration("COLLECTOR_MIN_WAIT_TIME", 3*time.Second),
},
CFFetcher: cffetcher.Config{
ClientConfig: &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,
},
},
RecordMinAge: getEnvWithDefaultDuration("CF_RECORD_MIN_AGE", 10*time.Minute),
FetchLimit: getEnvWithDefaultInt("CF_FETCH_LIMIT", 50),
},
Processor: ProcessorConfig{
Schedule: getEnvWithDefaultDuration("PROCESSOR_SCHEDULE", 240*time.Minute),
},
ServerPort: getEnvWithDefaultInt("PORT", 8881),
}
return cfg, nil
}
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 getEnvWithDefaultInt(k string, def int) int {
v := getEnvWithDefaultString(k, "")
if v == "" {
return def
}
n, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
return n
}
func getEnvWithDefaultString(k string, def string) string {
v := os.Getenv(k)
if v == "" {
return def
}
return v
}
func getEnvString(k string) string {
v := getEnvWithDefaultString(k, "")
if v == "" {
panic(fmt.Sprintf("environment variable %s is required", k))
}
return v
}
func getDefaultLogger() lager.Logger {
logger := lager.NewLogger("paas-billing")
logLevel := lager.INFO
if strings.ToLower(os.Getenv("LOG_LEVEL")) == "debug" {
logLevel = lager.DEBUG
}
logger.RegisterSink(lager.NewWriterSink(os.Stdout, logLevel))
return logger
}
func getwd() string {
pwd := os.Getenv("PWD")
if pwd == "" {
pwd, _ = os.Getwd()
}
return pwd
}