Pulse/internal/utils/helpers.go

43 lines
1.1 KiB
Go

package utils
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/google/uuid"
)
// GenerateID generates a unique ID with the given prefix
func GenerateID(prefix string) string {
return fmt.Sprintf("%s-%s", prefix, uuid.NewString())
}
// WriteJSONResponse writes a JSON response to the http.ResponseWriter
func WriteJSONResponse(w http.ResponseWriter, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
// Use Marshal instead of Encoder for better performance with large payloads
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
_, err = w.Write(jsonData)
return err
}
// ParseBool interprets common boolean strings, returning true for typical truthy values.
func ParseBool(value string) bool {
switch strings.ToLower(strings.TrimSpace(value)) {
case "1", "true", "yes", "y", "on":
return true
default:
return false
}
}
// GetenvTrim returns the environment variable value with surrounding whitespace removed.
func GetenvTrim(key string) string {
return strings.TrimSpace(os.Getenv(key))
}