From 6a9de539cefeb854f5d639c02623adf01e5aac84 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 3 Apr 2026 13:02:10 +0300 Subject: [PATCH] initial --- .gitignore | 1 + cmd/main.go | 25 +++++++++++ config/config.go | 36 ++++++++++++++++ config/helper.go | 52 +++++++++++++++++++++++ config/logging.go | 92 +++++++++++++++++++++++++++++++++++++++++ go.mod | 7 ++++ go.sum | 12 ++++++ internal/app/handler.go | 44 ++++++++++++++++++++ mandarake.env | 8 ++++ 9 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/main.go create mode 100644 config/config.go create mode 100644 config/helper.go create mode 100644 config/logging.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/app/handler.go create mode 100644 mandarake.env diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..7977688 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "context" + log "github.com/sirupsen/logrus" + "os" + "os/signal" + "parser-mandarake/config" + "parser-mandarake/internal/app" + "syscall" +) + +func main() { + c := config.New() + config.LogSetup(c.App.Mode, c.App.LogLvl) + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer cancel() + + appl := app.NewApp(c) + + if err := appl.Run(ctx); err != nil { + log.WithError(err).Fatal("Application run failed") + } +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..afab0fc --- /dev/null +++ b/config/config.go @@ -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"), + }, + } +} diff --git a/config/helper.go b/config/helper.go new file mode 100644 index 0000000..1771dbe --- /dev/null +++ b/config/helper.go @@ -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 +} diff --git a/config/logging.go b/config/logging.go new file mode 100644 index 0000000..032ed8a --- /dev/null +++ b/config/logging.go @@ -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 + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d74ce1a --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module parser-mandarake + +go 1.26.1 + +require github.com/sirupsen/logrus v1.9.4 + +require golang.org/x/sys v0.42.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..27a5315 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= +github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= +golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/app/handler.go b/internal/app/handler.go new file mode 100644 index 0000000..c481c6d --- /dev/null +++ b/internal/app/handler.go @@ -0,0 +1,44 @@ +package app + +import ( + "context" + log "github.com/sirupsen/logrus" + "parser-mandarake/config" + "time" +) + +const AppName string = "Mandarake parser" + +type App struct { + mode string +} + +func NewApp(cfg *config.Config) *App { + a := App{ + mode: cfg.App.Mode, + } + + log.Infof("%v: %v", AppName, a.mode) + return &a +} + +func (app *App) Run(ctx context.Context) error { + log.Info("App started") + errChan := make(chan error, 3) + + select { + case <-ctx.Done(): + return app.Shutdown(ctx) + case err := <-errChan: + return err + } +} + +func (app *App) Shutdown(ctx context.Context) error { + log.Info("App shutting down") + ctx, cancel := context.WithTimeout(ctx, time.Second*10) + defer cancel() + + _ = ctx + return nil +} diff --git a/mandarake.env b/mandarake.env new file mode 100644 index 0000000..20a910d --- /dev/null +++ b/mandarake.env @@ -0,0 +1,8 @@ +APP_MODE=dev +APP_LOG_LVL=debug + +RABBIT_HOST= +RABBIT_PORT= +RABBIT_USER= +RABBIT_PASS= +RABBIT_VHOST=