This commit is contained in:
nquidox 2026-04-03 13:02:10 +03:00
commit 6a9de539ce
9 changed files with 277 additions and 0 deletions

36
config/config.go Normal file
View file

@ -0,0 +1,36 @@
package config
type Config struct {
App AppConfig
Rabbit RabbitMQConfig
}
type AppConfig struct {
Mode string
LogLvl string
}
type RabbitMQConfig struct {
Host string
Port uint16
User string
Pass string
Vhost string
}
func New() *Config {
return &Config{
App: AppConfig{
Mode: getEnv("APP_MODE", "dev"),
LogLvl: getEnv("APP_LOG_LVL", "debug"),
},
Rabbit: RabbitMQConfig{
Host: getEnv("RABBIT_HOST", "10.0.0.4"),
Port: getEnvPort("RABBIT_PORT", 5672),
User: getEnv("RABBIT_USER", "parser-mandarake-dev"),
Pass: getEnv("RABBIT_PASS", "dev-pass"),
Vhost: getEnv("RABBIT_VHOST", "taskProcessorDevHost"),
},
}
}

52
config/helper.go Normal file
View file

@ -0,0 +1,52 @@
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
}
func getEnvPort(key string, fallback uint16) uint16 {
if value, ok := os.LookupEnv(key); ok {
num, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return fallback
}
return uint16(num)
}
return fallback
}

92
config/logging.go Normal file
View file

@ -0,0 +1,92 @@
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", errVal)
}
fieldsLine := "\n"
fields := entry.Data
if len(fields) > 0 {
fieldsLine = "| params: "
for key, val := range fields {
fieldsLine += fmt.Sprintf("\t%v=%v ", key, val)
}
}
cyanCode := f.getLevelColor(logrus.TraceLevel)
filename := fmt.Sprintf("%s[%s:%d]%s", cyanCode, file, line, resetCode)
logLine := fmt.Sprintf("%s[%s]%v %s\t%v %v",
coloredLevel, timestamp, filename, msg, fieldsLine, 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
}
}