Add GatewayConfig

Add env configurable readTimeout / writeTimeout
Add env-timeout test
Modify appropriate Dockerfiles

Signed-off-by: leigh schrandt <leigh@null.net>
This commit is contained in:
leigh schrandt
2017-07-02 04:47:29 -06:00
committed by Alex Ellis
parent a9774a9c2a
commit 457d0be78b
7 changed files with 167 additions and 31 deletions

61
gateway/readconfig.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"os"
"strconv"
"time"
)
// OsEnv implements interface to wrap os.Getenv
type OsEnv struct {
}
// Getenv wraps os.Getenv
func (OsEnv) Getenv(key string) string {
return os.Getenv(key)
}
// HasEnv provides interface for os.Getenv
type HasEnv interface {
Getenv(key string) string
}
// ReadConfig constitutes config from env variables
type ReadConfig struct {
}
func parseBoolValue(val string) bool {
if val == "true" {
return true
}
return false
}
func parseIntValue(val string, fallback int) int {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return parsedVal
}
}
return fallback
}
// Read fetches config from environmental variables.
func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
cfg := GatewayConfig{}
readTimeout := parseIntValue(hasEnv.Getenv("read_timeout"), 8)
writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout"), 8)
cfg.readTimeout = time.Duration(readTimeout) * time.Second
cfg.writeTimeout = time.Duration(writeTimeout) * time.Second
return cfg
}
// GatewayConfig for the process.
type GatewayConfig struct {
readTimeout time.Duration
writeTimeout time.Duration
}