initialize project structure with main, routing, and environment utilities

This commit is contained in:
2025-07-17 04:01:30 +08:00
parent 47db58b967
commit 70b653a88b
6 changed files with 79 additions and 0 deletions

46
pkg/utils/env.go Normal file
View File

@ -0,0 +1,46 @@
package utils
import (
"github.com/joho/godotenv"
"os"
"strconv"
)
func init() {
_ = godotenv.Load()
}
func Getenv(key string, defaultValue ...string) string {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return value
}
func GetenvAsInt(key string, defaultValue ...int) int {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
intValue, err := strconv.Atoi(value)
if err != nil && len(defaultValue) > 0 {
return defaultValue[0]
}
return intValue
}
func GetenvAsBool(key string, defaultValue ...bool) bool {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
boolValue, err := strconv.ParseBool(value)
if err != nil {
if len(defaultValue) > 0 {
return defaultValue[0]
}
return false
}
return boolValue
}