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

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

25
cmd/main.go Normal file
View file

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

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

7
go.mod Normal file
View file

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

12
go.sum Normal file
View file

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

44
internal/app/handler.go Normal file
View file

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

8
mandarake.env Normal file
View file

@ -0,0 +1,8 @@
APP_MODE=dev
APP_LOG_LVL=debug
RABBIT_HOST=
RABBIT_PORT=
RABBIT_USER=
RABBIT_PASS=
RABBIT_VHOST=