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 }