Files
goma-gateway/util/helpers.go

160 lines
3.1 KiB
Go
Raw Normal View History

2024-10-27 06:10:27 +01:00
package util
/*
Copyright 2024 Jonas Kaninda.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may get a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
import (
"net/url"
2024-10-27 06:10:27 +01:00
"os"
2024-11-10 14:52:31 +01:00
"regexp"
2024-10-27 06:10:27 +01:00
"strconv"
"strings"
2024-11-12 12:38:34 +01:00
"time"
"github.com/robfig/cron/v3"
2024-10-27 06:10:27 +01:00
)
// FileExists checks if the file does exist
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// FolderExists checks if the folder does exist
func FolderExists(name string) bool {
info, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
2024-10-27 06:10:27 +01:00
func GetStringEnv(key, defaultValue string) string {
val := os.Getenv(key)
if val == "" {
return defaultValue
}
return val
}
func GetIntEnv(key string, defaultValue int) int {
val := os.Getenv(key)
if val == "" {
return defaultValue
}
i, err := strconv.Atoi(val)
if err != nil {
return defaultValue
}
return i
}
func GetBoolEnv(key string, defaultValue bool) bool {
val := os.Getenv(key)
if val == "" {
return defaultValue
}
b, err := strconv.ParseBool(val)
if err != nil {
return defaultValue
}
return b
}
// SetEnv Set env
func SetEnv(name, value string) {
if len(value) != 0 {
err := os.Setenv(name, value)
if err != nil {
return
}
2024-10-27 06:10:27 +01:00
}
}
func MergeSlices(slice1, slice2 []string) []string {
return append(slice1, slice2...)
}
2024-11-16 10:10:35 +01:00
// ParseURLPath removes duplicated [//]
2024-10-27 06:10:27 +01:00
func ParseURLPath(urlPath string) string {
// Replace any double slashes with a single slash
urlPath = strings.ReplaceAll(urlPath, "//", "/")
// Ensure the path starts with a single leading slash
if !strings.HasPrefix(urlPath, "/") {
urlPath = "/" + urlPath
}
return urlPath
}
func ParseRoutePath(path, blockedPath string) string {
basePath := ParseURLPath(path)
switch {
case blockedPath == "":
return basePath
case strings.HasSuffix(blockedPath, "/*"):
return basePath + blockedPath[:len(blockedPath)-2]
case strings.HasSuffix(blockedPath, "*"):
return basePath + blockedPath[:len(blockedPath)-1]
default:
return basePath + blockedPath
}
}
func UrlParsePath(uri string) string {
parse, err := url.Parse(uri)
if err != nil {
return ""
}
return parse.Path
}
2024-11-10 14:52:31 +01:00
func HasWhitespace(s string) bool {
return regexp.MustCompile(`\s`).MatchString(s)
}
2024-11-12 12:38:34 +01:00
// IsValidCronExpression verify cronExpression and returns boolean
func IsValidCronExpression(cronExpr string) bool {
// Parse the cron expression
_, err := cron.ParseStandard(cronExpr)
return err == nil
}
func ParseDuration(durationStr string) (time.Duration, error) {
if durationStr == "" {
return 0, nil
}
duration, err := time.ParseDuration(durationStr)
if err != nil {
return 0, err
}
return duration, nil
}
2024-11-15 08:19:22 +01:00
func Slug(text string) string {
// Convert to lowercase
text = strings.ToLower(text)
// Replace spaces and special characters with hyphens
re := regexp.MustCompile(`\W+`)
2024-11-15 08:19:22 +01:00
text = re.ReplaceAllString(text, "-")
// Remove leading and trailing hyphens
text = strings.Trim(text, "-")
return text
}