Add api environment variables as constants for consistency

This commit is contained in:
Hasyimi Bahrudin 2016-08-03 14:11:04 +08:00
parent bb6bb19aad
commit 105cea9bae
2 changed files with 30 additions and 14 deletions

View File

@ -24,6 +24,22 @@ const (
// HTTPAddrEnvName defines an environment variable name which sets // HTTPAddrEnvName defines an environment variable name which sets
// the HTTP address if there is no -http-addr specified. // the HTTP address if there is no -http-addr specified.
HTTPAddrEnvName = "CONSUL_HTTP_ADDR" HTTPAddrEnvName = "CONSUL_HTTP_ADDR"
// HTTPTokenEnvName defines an environment variable name which sets
// the HTTP token.
HTTPTokenEnvName = "CONSUL_HTTP_TOKEN"
// HTTPAuthEnvName defines an environment variable name which sets
// the HTTP authentication header.
HTTPAuthEnvName = "CONSUL_HTTP_AUTH"
// HTTPSSLEnvName defines an environment variable name which sets
// whether or not to use HTTPS.
HTTPSSLEnvName = "CONSUL_HTTP_SSL"
// HTTPSSLVerifyEnvName defines an environment variable name which sets
// whether or not to disable certificate checking.
HTTPSSLVerifyEnvName = "CONSUL_HTTP_SSL_VERIFY"
) )
// QueryOptions are used to parameterize a query // QueryOptions are used to parameterize a query
@ -188,11 +204,11 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
config.Address = addr config.Address = addr
} }
if token := os.Getenv("CONSUL_HTTP_TOKEN"); token != "" { if token := os.Getenv(HTTPTokenEnvName); token != "" {
config.Token = token config.Token = token
} }
if auth := os.Getenv("CONSUL_HTTP_AUTH"); auth != "" { if auth := os.Getenv(HTTPAuthEnvName); auth != "" {
var username, password string var username, password string
if strings.Contains(auth, ":") { if strings.Contains(auth, ":") {
split := strings.SplitN(auth, ":", 2) split := strings.SplitN(auth, ":", 2)
@ -208,10 +224,10 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
} }
} }
if ssl := os.Getenv("CONSUL_HTTP_SSL"); ssl != "" { if ssl := os.Getenv(HTTPSSLEnvName); ssl != "" {
enabled, err := strconv.ParseBool(ssl) enabled, err := strconv.ParseBool(ssl)
if err != nil { if err != nil {
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL: %s", err) log.Printf("[WARN] client: could not parse %s: %s", HTTPSSLEnvName, err)
} }
if enabled { if enabled {
@ -219,10 +235,10 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
} }
} }
if verify := os.Getenv("CONSUL_HTTP_SSL_VERIFY"); verify != "" { if verify := os.Getenv(HTTPSSLVerifyEnvName); verify != "" {
doVerify, err := strconv.ParseBool(verify) doVerify, err := strconv.ParseBool(verify)
if err != nil { if err != nil {
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL_VERIFY: %s", err) log.Printf("[WARN] client: could not parse %s: %s", HTTPSSLVerifyEnvName, err)
} }
if !doVerify { if !doVerify {

View File

@ -78,14 +78,14 @@ func TestDefaultConfig_env(t *testing.T) {
os.Setenv(HTTPAddrEnvName, addr) os.Setenv(HTTPAddrEnvName, addr)
defer os.Setenv(HTTPAddrEnvName, "") defer os.Setenv(HTTPAddrEnvName, "")
os.Setenv("CONSUL_HTTP_TOKEN", token) os.Setenv(HTTPTokenEnvName, token)
defer os.Setenv("CONSUL_HTTP_TOKEN", "") defer os.Setenv(HTTPTokenEnvName, "")
os.Setenv("CONSUL_HTTP_AUTH", auth) os.Setenv(HTTPAuthEnvName, auth)
defer os.Setenv("CONSUL_HTTP_AUTH", "") defer os.Setenv(HTTPAuthEnvName, "")
os.Setenv("CONSUL_HTTP_SSL", "1") os.Setenv(HTTPSSLEnvName, "1")
defer os.Setenv("CONSUL_HTTP_SSL", "") defer os.Setenv(HTTPSSLEnvName, "")
os.Setenv("CONSUL_HTTP_SSL_VERIFY", "0") os.Setenv(HTTPSSLVerifyEnvName, "0")
defer os.Setenv("CONSUL_HTTP_SSL_VERIFY", "") defer os.Setenv(HTTPSSLVerifyEnvName, "")
for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} { for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} {
if config.Address != addr { if config.Address != addr {