feat: added server and db config
This commit is contained in:
33
server/utils/env.go
Normal file
33
server/utils/env.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Gets value from environment or uses default value
|
||||
func GetEnvOrDefault[T string | int | bool | float64](key string, defaultValue T) T {
|
||||
value, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// Handle conversion based on the type of defaultValue
|
||||
switch any(defaultValue).(type) {
|
||||
case string:
|
||||
return any(value).(T)
|
||||
case int:
|
||||
if intValue, err := strconv.Atoi(value); err == nil {
|
||||
return any(intValue).(T)
|
||||
}
|
||||
case bool:
|
||||
if boolValue, err := strconv.ParseBool(value); err == nil {
|
||||
return any(boolValue).(T)
|
||||
}
|
||||
case float64:
|
||||
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
||||
return any(floatValue).(T)
|
||||
}
|
||||
}
|
||||
return defaultValue // return default value if conversion fails
|
||||
}
|
||||
Reference in New Issue
Block a user