This commit is contained in:
2025-11-03 12:24:01 +02:00
commit 0806865287
177 changed files with 18453 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
// pkg/logger/logger.go
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var log *zap.Logger
func init() {
config := zap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
var err error
log, err = config.Build()
if err != nil {
panic(err)
}
}
func Info(msg string, fields ...zap.Field) {
log.Info(msg, fields...)
}
func Error(msg string, fields ...zap.Field) {
log.Error(msg, fields...)
}
func Fatal(msg string, fields ...zap.Field) {
log.Fatal(msg, fields...)
}
func With(fields ...zap.Field) *zap.Logger {
return log.With(fields...)
}

View File

@@ -0,0 +1,67 @@
// pkg/validator/validator.go
package validator
import (
"log"
"regexp"
"strings"
"github.com/go-playground/validator/v10"
)
var (
validate *validator.Validate
emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
)
func init() {
validate = validator.New()
// Register custom validation
err := validate.RegisterValidation("email", validateEmail)
if err != nil {
log.Print(err)
}
}
func validateEmail(fl validator.FieldLevel) bool {
return emailRegex.MatchString(fl.Field().String())
}
// ValidationError represents a validation error
type ValidationError struct {
Field string `json:"field"`
Message string `json:"message"`
}
// Validate validates a struct and returns validation errors
func Validate(i interface{}) []ValidationError {
var errors []ValidationError
err := validate.Struct(i)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
var element ValidationError
element.Field = strings.ToLower(err.Field())
element.Message = generateValidationMessage(err)
errors = append(errors, element)
}
}
return errors
}
func generateValidationMessage(err validator.FieldError) string {
switch err.Tag() {
case "required":
return "This field is required"
case "email":
return "Invalid email format"
case "min":
return "Value must be greater than " + err.Param()
case "max":
return "Value must be less than " + err.Param()
default:
return "Invalid value"
}
}