This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfiguration.go
182 lines (151 loc) · 5.15 KB
/
configuration.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package configuration
import (
"fmt"
"strings"
commoncfg "github.com/fabric8-services/fabric8-common/configuration"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)
const (
// Constants for viper variable names. Will be used to set
// default values as well as to get each value
varHTTPAddress = "http.address"
varMetricsHTTPAddress = "metrics.http.address"
varDeveloperModeEnabled = "developer.mode.enabled"
varWITURL = "wit.url"
varAuthURL = "auth.url"
varMadrillAPIKey = "mandrill.apikey"
varLogLevel = "log.level"
varLogJSON = "log.json"
varServiceAccountID = "service.account.id"
varServiceAccountSecret = "service.account.secret"
)
// Data encapsulates the Viper configuration object which stores the configuration data in-memory.
type Data struct {
v *viper.Viper
}
// NewData creates a configuration reader object using a configurable configuration file path
func NewData() (*Data, error) {
c := Data{
v: viper.New(),
}
c.v.SetEnvPrefix("F8")
c.v.AutomaticEnv()
c.v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
c.v.SetTypeByDefaultValue(true)
c.setConfigDefaults()
return &c, nil
}
// String returns the current configuration as a string
func (c *Data) String() string {
allSettings := c.v.AllSettings()
y, err := yaml.Marshal(&allSettings)
if err != nil {
log.WithFields(map[string]interface{}{
"settings": allSettings,
"err": err,
}).Panicln("Failed to marshall config to string")
}
return fmt.Sprintf("%s\n", y)
}
// GetData is a wrapper over NewData which reads configuration file path
// from the environment variable.
func GetData() (*Data, error) {
cd, err := NewData()
return cd, err
}
func (c *Data) setConfigDefaults() {
c.v.SetTypeByDefaultValue(true)
//-----
// HTTP
//-----
c.v.SetDefault(varHTTPAddress, "0.0.0.0:8080")
c.v.SetDefault(varMetricsHTTPAddress, "0.0.0.0:8080")
c.v.SetDefault(varWITURL, defaultWITURL)
c.v.SetDefault(varAuthURL, defaultAuthURL)
//-----
// Misc
//-----
// Enable development related features, e.g. token generation endpoint
c.v.SetDefault(varDeveloperModeEnabled, false)
c.v.SetDefault(varLogLevel, defaultLogLevel)
c.v.SetDefault(varServiceAccountID, "4c34f6d4-f00b-487b-9a1f-e7d1adba6866")
c.v.SetDefault(varServiceAccountSecret, "secret")
// c.v.SetDefault(varMadrillAPIKey, "1234") // Enable for local testing.
}
// GetHTTPAddress returns the HTTP address (as set via default, config file, or environment variable)
// that the notification server binds to (e.g. "0.0.0.0:8080")
func (c *Data) GetHTTPAddress() string {
return c.v.GetString(varHTTPAddress)
}
// GetMetricsHTTPAddress returns the address the /metrics endpoing will be mounted.
// By default GetMetricsHTTPAddress is the same as GetHTTPAddress
func (c *Data) GetMetricsHTTPAddress() string {
return c.v.GetString(varMetricsHTTPAddress)
}
// IsDeveloperModeEnabled returns if development related features (as set via default, config file, or environment variable),
// e.g. token generation endpoint are enabled
func (c *Data) IsDeveloperModeEnabled() bool {
return c.v.GetBool(varDeveloperModeEnabled)
}
// GetWITURL return the base WorkItemTracker API URL
func (c *Data) GetWITURL() string {
return c.v.GetString(varWITURL)
}
// GetAuthServiceURL return the base Auth API URL
func (c *Data) GetAuthServiceURL() string {
return c.v.GetString(varAuthURL)
}
// GetServiceAccountID returns service account ID for the notification service.
// This will be used by the notification service to request for a service account token
// from the Auth service.
func (c *Data) GetServiceAccountID() string {
return c.v.GetString(varServiceAccountID)
}
// GetServiceAccountSecret returns service account secret for the notification service.
// This will be used by the notification service to request for a service account token
// from the Auth service.
func (c *Data) GetServiceAccountSecret() string {
return c.v.GetString(varServiceAccountSecret)
}
// GetWebURL returns the base URL for the Web v
func (c *Data) GetWebURL() string {
return strings.Replace(c.GetWITURL(), "api.", "", -1)
}
// GetMadrillAPIKey returns the API key used by the email sender
func (c *Data) GetMadrillAPIKey() string {
return c.v.GetString(varMadrillAPIKey)
}
// GetLogLevel returns the loggging level (as set via config file or environment variable)
func (c *Data) GetLogLevel() string {
return c.v.GetString(varLogLevel)
}
// IsLogJSON returns if we should log json format (as set via config file or environment variable)
func (c *Data) IsLogJSON() bool {
if c.v.IsSet(varLogJSON) {
return c.v.GetBool(varLogJSON)
}
if c.IsDeveloperModeEnabled() {
return false
}
return true
}
func (c *Data) Validate() error {
if c.GetMadrillAPIKey() == "" {
return fmt.Errorf("Missing %v", varMadrillAPIKey)
}
return nil
}
// GetDevModePrivateKey returns the private key and its ID used in tests
func (c *Data) GetDevModePrivateKey() []byte {
if c.IsDeveloperModeEnabled() {
return []byte(commoncfg.DevModeRsaPrivateKey)
}
return nil
}
const (
defaultWITURL = "https://api.openshift.io/"
defaultAuthURL = "http://localhost:8089/"
defaultLogLevel = "info"
)