new fields

This commit is contained in:
nquidox 2026-02-18 19:58:09 +03:00
parent ca36d84a03
commit f6e99b2b65
3 changed files with 41 additions and 15 deletions

View file

@ -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),
},
}
}

View file

@ -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
}