diff --git a/config.env b/config.env index e69de29..0d78512 100644 --- a/config.env +++ b/config.env @@ -0,0 +1,12 @@ +#[App] +APP_LOG_LVL= + +#[Http] +HTTP_HOST= +HTTP_PORT= +GIN_MODE= + +#[gRPC] +TASK_API_HOST= +TASK_API_PORT= +TASK_SOURCE_TIMEOUT_SECONDS= diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..3c2b8c4 --- /dev/null +++ b/config/config.go @@ -0,0 +1,45 @@ +package config + +import "time" + +type Config struct { + App App + Http Http + TasksSource TasksSource +} + +type App struct { + LogLvl string +} + +type Http struct { + Host string + Port string + GinMode string +} + +type TasksSource struct { + Host string + Port string + Timeout time.Duration +} + +func NewConfig() Config { + return Config{ + App: App{ + LogLvl: getEnv("APP_LOG_LVL", "debug"), + }, + + Http: Http{ + Host: getEnv("HTTP_HOST", "0.0.0.0"), + Port: getEnv("HTTP_PORT", "41082"), + GinMode: getEnv("GIN_MODE", "dev"), + }, + + TasksSource: TasksSource{ + Host: getEnv("TASK_API_HOST", "127.0.0.1"), + Port: getEnv("TASK_API_PORT", "61000"), + Timeout: getEnvSeconds("TASK_SOURCE_TIMEOUT_SECONDS", 60), + }, + } +} diff --git a/config/helper.go b/config/helper.go new file mode 100644 index 0000000..e17dc02 --- /dev/null +++ b/config/helper.go @@ -0,0 +1,41 @@ +package config + +import ( + log "github.com/sirupsen/logrus" + "os" + "strconv" + "time" +) + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + 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 +} + +func getEnvUint(key string, fallback uint) uint { + var def uint = 100 + if value, ok := os.LookupEnv(key); ok { + num, err := strconv.ParseUint(value, 10, 64) + if err != nil { + log.Printf("Error converting %v to uint, using default value - %v", key, def) + return def + } + return uint(num) + } + return fallback +} diff --git a/config/logging.go b/config/logging.go new file mode 100644 index 0000000..d433839 --- /dev/null +++ b/config/logging.go @@ -0,0 +1,83 @@ +package config + +import ( + "fmt" + "github.com/sirupsen/logrus" + "os" + "path/filepath" + "strings" +) + +func LogSetup(mode, lvl string) { + l, err := logrus.ParseLevel(lvl) + if err != nil { + l = logrus.InfoLevel + } + + logrus.SetLevel(l) + + switch mode { + case "release": + case "dev": + { + logrus.SetReportCaller(true) + logrus.SetFormatter(&CustomFormatter{}) + logrus.SetOutput(os.Stdout) + } + } +} + +type CustomFormatter struct{} + +func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) { + timestamp := entry.Time.Format("2006-01-02 15:04:05") + level := strings.ToUpper(entry.Level.String()) + msg := entry.Message + + file := "" + line := 0 + if entry.Caller != nil { + file = filepath.Base(entry.Caller.File) + line = entry.Caller.Line + } + + colorCode := f.getLevelColor(entry.Level) + resetCode := "\033[0m" + coloredLevel := fmt.Sprintf("%s[%s]%s", colorCode, level, resetCode) + + errVal := entry.Data["error"] + if errVal == nil { + errVal = "\n" + } else { + errVal = fmt.Sprintf("---%v\n\n", errVal) + } + + cyanCode := f.getLevelColor(logrus.TraceLevel) + filename := fmt.Sprintf("%s[%s:%d]%s", cyanCode, file, line, resetCode) + + logLine := fmt.Sprintf("%s[%s]%v %s\n%v", + coloredLevel, timestamp, filename, msg, errVal) + + return []byte(logLine), nil +} + +func (f *CustomFormatter) getLevelColor(level logrus.Level) string { + switch level { + case logrus.PanicLevel: + return "\033[35m" // Magenta + case logrus.FatalLevel: + return "\033[35m" // Magenta + case logrus.ErrorLevel: + return "\033[31m" // Red + case logrus.WarnLevel: + return "\033[33m" // Yellow + case logrus.InfoLevel: + return "\033[32m" // Green + case logrus.DebugLevel: + return "\033[34m" // Blue + case logrus.TraceLevel: + return "\033[36m" // Cyan + default: + return "\033[0m" // No color + } +}