updates to internal config module
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
@ -13,21 +12,21 @@ import (
|
||||
|
||||
type Config struct {
|
||||
// time configuration
|
||||
TimeFormat string `env:"TIME_FORMAT" default:"2006-01-02 15:04:05"`
|
||||
TimeZoneLocal string `env:"TIME_ZONE" default:"America/Chicago"`
|
||||
TZoneLocal *time.Location
|
||||
TZoneUTC *time.Location
|
||||
TimeFormat string `env:"time_format" default:"2006-01-02 15:04:05"`
|
||||
TimeZoneLocal string `env:"time_zone" default:"America/Chicago"`
|
||||
TZoneLocal *time.Location `ignored:"true"`
|
||||
TZoneUTC *time.Location `ignored:"true"`
|
||||
|
||||
// logging
|
||||
LogLevel int `env:"LOG_LEVEL" default:"50"`
|
||||
Log *logutils.LevelFilter
|
||||
|
||||
// webserver
|
||||
WebServerPort int `env:"WEBSERVER_PORT" default:"8080"`
|
||||
WebServerIP string `env:"WEBSERVER_IP" default:"0.0.0.0"`
|
||||
WebServerReadTimeout int `env:"WEBSERVER_READ_TIMEOUT" default:"5"`
|
||||
WebServerWriteTimeout int `env:"WEBSERVER_WRITE_TIMEOUT" default:"1"`
|
||||
WebServerIdleTimeout int `env:"WEBSERVER_IDLE_TIMEOUT" default:"2"`
|
||||
WebServerPort int `env:"webserver_port" default:"8080"`
|
||||
WebServerIP string `env:"webserver_ip" default:"0.0.0.0"`
|
||||
WebServerReadTimeout int `env:"webserver_read_timeout" default:"5"`
|
||||
WebServerWriteTimeout int `env:"webserver_write_timeout" default:"1"`
|
||||
WebServerIdleTimeout int `env:"webserver_idle_timeout" default:"2"`
|
||||
}
|
||||
|
||||
// DefaultConfig initializes the config variable for use with a prepared set of defaults.
|
||||
@ -40,6 +39,7 @@ func DefaultConfig() *Config {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func (cfg *Config) validate() error {
|
||||
checks := []struct {
|
||||
bad bool
|
||||
@ -62,6 +62,7 @@ func (cfg *Config) validate() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (cfg *Config) setLogLevel() {
|
||||
switch {
|
||||
|
@ -63,11 +63,13 @@ func getStructInfo(spec interface{}) ([]StructInfo, error) {
|
||||
}
|
||||
info.Key = strings.ToUpper(info.Key)
|
||||
if ftype.Tag.Get("default") != "" {
|
||||
v, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("default"))
|
||||
v, err := typeConversion(ftype.Type.String(), getOSEnv(ftype.Tag.Get("env"), ftype.Tag.Get("default")))
|
||||
if err != nil {
|
||||
return []StructInfo{}, err
|
||||
}
|
||||
info.DefaultValue = v
|
||||
} else {
|
||||
info.DefaultValue = getOSEnv(ftype.Tag.Get("env"), "")
|
||||
}
|
||||
infos = append(infos, info)
|
||||
}
|
||||
|
@ -5,55 +5,15 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getEnvString returns string from environment variable
|
||||
func getEnvString(env, def string) (val string) { //nolint:deadcode
|
||||
val = os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
func getOSEnv(env, def string) string {
|
||||
if val, ok := os.LookupEnv(strings.ToUpper(env)); ok {
|
||||
return val
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getEnvInt returns int from environment variable
|
||||
func getEnvInt(env string, def int) (ret int) {
|
||||
val := os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
ret, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not numeric: %v\n", env)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getEnvBool returns boolean from environment variable
|
||||
func getEnvBool(env string, def bool) bool {
|
||||
var (
|
||||
err error
|
||||
retVal bool
|
||||
val = os.Getenv(env)
|
||||
)
|
||||
|
||||
if len(val) == 0 {
|
||||
return def
|
||||
} else {
|
||||
retVal, err = strconv.ParseBool(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not boolean: %v\n", env)
|
||||
}
|
||||
}
|
||||
|
||||
return retVal
|
||||
return def
|
||||
}
|
||||
|
||||
// Init initializes the application configuration by reading default values from the struct's tags
|
||||
@ -71,28 +31,27 @@ func Init() *Config {
|
||||
switch info.Type.String() {
|
||||
case "string":
|
||||
var dv string
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = info.DefaultValue.(string)
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string)
|
||||
flag.StringVar(p, info.Name, getEnvString(info.Name, dv), "("+info.Key+")")
|
||||
flag.StringVar(p, info.Name, dv, "("+info.Key+")")
|
||||
|
||||
case "bool":
|
||||
var dv bool
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = info.DefaultValue.(bool)
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool)
|
||||
flag.BoolVar(p, info.Name, getEnvBool(info.Name, dv), "("+info.Key+")")
|
||||
flag.BoolVar(p, info.Name, dv, "("+info.Key+")")
|
||||
|
||||
case "int":
|
||||
var dv int
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = int(info.DefaultValue.(int64))
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*int)
|
||||
flag.IntVar(p, info.Name, getEnvInt(info.Name, dv), "("+info.Key+")")
|
||||
flag.IntVar(p, info.Name, dv, "("+info.Key+")")
|
||||
}
|
||||
}
|
||||
flag.Parse()
|
||||
@ -100,12 +59,6 @@ func Init() *Config {
|
||||
// set logging level
|
||||
cfg.setLogLevel()
|
||||
|
||||
// validate some required values are defined.
|
||||
// need to break this out to a required:"true" struct tag
|
||||
if err = cfg.validate(); err != nil {
|
||||
log.Fatalf("[FATAL] %v", err)
|
||||
}
|
||||
|
||||
// timezone & format configuration
|
||||
cfg.TZoneUTC, _ = time.LoadLocation("UTC")
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user