forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetEnv(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
if p := os.Getenv(key + "_FILE"); p != "" {
|
||||
if b, err := os.ReadFile(p); err == nil {
|
||||
return strings.TrimSpace(string(b))
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func ParseIntEnv(key string, fallback int) int {
|
||||
v := GetEnv(key, "")
|
||||
if v == "" {
|
||||
return fallback
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func ParseDurationSecondsEnv(key string, fallbackSeconds int) time.Duration {
|
||||
n := ParseIntEnv(key, fallbackSeconds)
|
||||
return time.Duration(n) * time.Second
|
||||
}
|
||||
Reference in New Issue
Block a user