diff --git a/config.env b/config.env index 7f3743d..aef7254 100644 --- a/config.env +++ b/config.env @@ -1,13 +1,15 @@ #[APP] APP_MODE=dev APP_LOG_LVL=info +APP_CHECK_PERIOD_SECONDS=21600 #[HTTP] -HOST=0.0.0.0 -PORT=41082 +HTTP_HOST=0.0.0.0 +HTTP_PORT=41082 #[TASKS SOURCE(GRPC)] -HOST= -PORT= +TASK_SOURCE_HOST= +TASK_SOURCE_PORT= +TASK_SOURCE_TIMEOUT_SECONDS= #[RABBIT] diff --git a/config/config.go b/config/config.go index a60585e..0161724 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,7 @@ package config +import "time" + type Config struct { App App Http Http @@ -7,8 +9,9 @@ type Config struct { } type App struct { - Mode string - LogLvl string + Mode string + LogLvl string + CheckPeriod time.Duration } type Http struct { @@ -17,25 +20,28 @@ type Http struct { } type TasksSource struct { - Host string - Port string + Host string + Port string + Timeout time.Duration } func NewConfig() Config { return Config{ App: App{ - Mode: getEnv("APP_MODE", "dev"), - LogLvl: getEnv("APP_LOG_LVL", "debug"), + Mode: getEnv("APP_MODE", "dev"), + LogLvl: getEnv("APP_LOG_LVL", "debug"), + CheckPeriod: getEnvSeconds("APP_CHECK_PERIOD_SECONDS", 20), }, Http: Http{ - Host: getEnv("HOST", "0.0.0.0"), - Port: getEnv("PORT", "41082"), + Host: getEnv("HTTP_HOST", "0.0.0.0"), + Port: getEnv("HTTP_PORT", "41082"), }, TasksSource: TasksSource{ - Host: getEnv("TASK_API_HOST", ""), - Port: getEnv("TASK_API_PORT", ""), + Host: getEnv("TASK_API_HOST", ""), + Port: getEnv("TASK_API_PORT", ""), + Timeout: getEnvSeconds("TASK_SOURCE_TIMEOUT_SECONDS", 60), }, } } diff --git a/config/helper.go b/config/helper.go index ea28dbc..d3849db 100644 --- a/config/helper.go +++ b/config/helper.go @@ -1,6 +1,11 @@ package config -import "os" +import ( + log "github.com/sirupsen/logrus" + "os" + "strconv" + "time" +) func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { @@ -8,3 +13,16 @@ func getEnv(key, fallback string) string { } return fallback } + +func getEnvSeconds(key string, fallback int) time.Duration { + if value, ok := os.LookupEnv(key); ok { + num, err := strconv.Atoi(value) + if err != nil { + log.Printf("Error converting %v to int, using default value - 60 seconds", key) + return time.Duration(60) * time.Second + } + + return time.Duration(num) * time.Second + } + return time.Duration(fallback) * time.Second +}