Initial commit

This commit is contained in:
Jonas Kaninda
2024-10-27 06:10:27 +01:00
commit 1923506e0a
35 changed files with 2592 additions and 0 deletions

31
util/constants.go Normal file
View File

@@ -0,0 +1,31 @@
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 (
"os"
)
var Version string
func VERSION(def string) string {
build := os.Getenv("VERSION")
if build == "" {
return def
}
return build
}
func FullVersion() string {
ver := Version
if b := VERSION(""); b != "" {
return b
}
return ver
}

84
util/helpers.go Normal file
View File

@@ -0,0 +1,84 @@
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 (
"os"
"strconv"
"strings"
)
// 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()
}
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) {
err := os.Setenv(name, value)
if err != nil {
return
}
}
func MergeSlices(slice1, slice2 []string) []string {
return append(slice1, slice2...)
}
// ParseURLPath returns a URL path
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
}

1
util/util_test.go Normal file
View File

@@ -0,0 +1 @@
package util