From a9f64dd069ddb1bdf7ad86c575f14be88644ea78 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 8 Oct 2023 12:41:50 +0200 Subject: [PATCH] added utils and config --- go.mod | 14 +++++- go.sum | 32 ++++++++++++++ rsconfig/rsconfig.go | 23 ++++++++++ rsutils/rsutils.go | 89 ++++++++++++++++++++++++++++++++++++++ rsvalidator/rsvalidator.go | 7 +-- 5 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 rsconfig/rsconfig.go create mode 100644 rsutils/rsutils.go diff --git a/go.mod b/go.mod index 50ebdb2..7345106 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,25 @@ module git.ex.umbach.dev/Alex/roese-utils go 1.21.0 require ( + github.com/andybalholm/brotli v1.0.5 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.15.5 // indirect + github.com/gofiber/fiber/v2 v2.49.2 // indirect + github.com/google/uuid v1.3.1 // indirect + github.com/klauspost/compress v1.16.7 // indirect github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/rs/zerolog v1.31.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.49.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/crypto v0.7.0 // indirect golang.org/x/net v0.8.0 // indirect - golang.org/x/sys v0.6.0 // indirect + golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.8.0 // indirect ) diff --git a/go.sum b/go.sum index 7da2209..602d1ed 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,6 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -8,21 +11,50 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24= github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs= +github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE= +github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/rsconfig/rsconfig.go b/rsconfig/rsconfig.go new file mode 100644 index 0000000..2f5f16e --- /dev/null +++ b/rsconfig/rsconfig.go @@ -0,0 +1,23 @@ +package rsconfig + +import ( + "fmt" + "os" +) + +func CreateEnvConfigFileIfNotExists(content string) { + if os.Getenv("DOCKER") != "" { + return + } + + if _, err := os.Stat(".env"); os.IsNotExist(err) { + err := os.WriteFile(".env", []byte(content), 0644) + + if err != nil { + panic("Failed to create .env file, err: " + err.Error()) + } + + fmt.Println("Config .env was created. Please configure and start the server again.") + os.Exit(1) + } +} diff --git a/rsutils/rsutils.go b/rsutils/rsutils.go new file mode 100644 index 0000000..6a79e87 --- /dev/null +++ b/rsutils/rsutils.go @@ -0,0 +1,89 @@ +package rsutils + +import ( + "crypto/rand" + "encoding/json" + "errors" + "math/big" + + "git.ex.umbach.dev/Alex/roese-utils/rsvalidator" + "github.com/gofiber/fiber/v2" + "github.com/rs/zerolog/log" +) + +func MarshalJson(v any) string { + json, err := json.Marshal(v) + + if err != nil { + log.Error().Msgf("Failed to marshal json %s", err) + return "" + } + + return string(json) +} + +// generates a random session id with 36 characters +func GenerateSession() (string, error) { + var letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + + r := make([]byte, 36) + + for i := 0; i < 36; i++ { + num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters)))) + + if err != nil { + log.Error().Msgf("Failed to session: %v", err) + return "", err + } + + if i == 8 || i == 13 || i == 18 || i == 23 { + r[i] = 45 + } else { + r[i] = letters[num.Int64()] + } + } + + return string(r), nil +} + +func ParamsParserHelper(c *fiber.Ctx, params interface{}) error { + if err := c.ParamsParser(params); err != nil { + log.Error().Msgf("Failed to parse params, err: %s", err.Error()) + return errors.New("Failed to parse params") + } + + if errValidation := rsvalidator.ValidateStruct(params); errValidation != nil { + log.Error().Msgf("Failed to validate params, err: %v", errValidation) + return errors.New("Failed to validate params") + } + + return nil +} + +func BodyParserHelper(c *fiber.Ctx, body interface{}) error { + if err := c.BodyParser(body); err != nil { + log.Error().Msgf("Failed to parse body, err: %s", err.Error()) + return errors.New("Failed to parse body") + } + + if errValidation := rsvalidator.ValidateStruct(body); errValidation != nil { + log.Error().Msgf("Failed to validate body, err: %v", errValidation) + return errors.New("Failed to validate body") + } + + return nil +} + +func QueryParserHelper(c *fiber.Ctx, query interface{}) error { + if err := c.QueryParser(query); err != nil { + log.Error().Msgf("Failed to parse query, err: %s", err.Error()) + return errors.New("Failed to parse query") + } + + if errValidation := rsvalidator.ValidateStruct(query); errValidation != nil { + log.Error().Msgf("Failed to validate query, err: %v", errValidation) + return errors.New("Failed to validate query") + } + + return nil +} diff --git a/rsvalidator/rsvalidator.go b/rsvalidator/rsvalidator.go index e23f518..6c4c899 100644 --- a/rsvalidator/rsvalidator.go +++ b/rsvalidator/rsvalidator.go @@ -1,7 +1,6 @@ package rsvalidator import ( - "log" "regexp" "strings" @@ -31,11 +30,7 @@ func ValidateStruct(event interface{}) []*ErrorResponse { return errors } -func validateNumericString(fl validator.FieldLevel) bool { +func ValidateNumericString(fl validator.FieldLevel) bool { str := fl.Field().String() return strings.TrimSpace(str) != "" && regexp.MustCompile(`^[0-9]+$`).MatchString(str) } - -func test() { - log.Println("test") -}