This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/service/random_generator.go
Tim Wundenberg 521119fc02
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 44s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 48s
fix: refine logging
2024-12-04 22:04:32 +01:00

49 lines
888 B
Go

package service
import (
"me-fit/log"
"me-fit/types"
"crypto/rand"
"encoding/base64"
"github.com/google/uuid"
)
type RandomService interface {
Bytes(size int) ([]byte, error)
String(size int) (string, error)
UUID() (uuid.UUID, error)
}
type RandomServiceImpl struct {
}
func NewRandomServiceImpl() *RandomServiceImpl {
return &RandomServiceImpl{}
}
func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
log.Error("Error generating random bytes: %v", err)
return []byte{}, types.ErrInternal
}
return b, nil
}
func (r *RandomServiceImpl) String(size int) (string, error) {
bytes, err := r.Bytes(size)
if err != nil {
return "", types.ErrInternal
}
return base64.StdEncoding.EncodeToString(bytes), nil
}
func (r *RandomServiceImpl) UUID() (uuid.UUID, error) {
return uuid.NewRandom()
}