109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
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 GenerateCode(codeLength int) (string, error) {
|
|
var letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
r := make([]byte, codeLength)
|
|
|
|
for i := 0; i < codeLength; 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
|
|
}
|
|
|
|
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
|
|
}
|