mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-03 15:56:22 +00:00
51 lines
949 B
Go
51 lines
949 B
Go
package utils
|
|
|
|
import (
|
|
"github.com/joho/godotenv"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func init() {
|
|
_ = godotenv.Load()
|
|
}
|
|
|
|
type envType struct{}
|
|
|
|
var Env envType
|
|
|
|
func (e *envType) Get(key string, defaultValue ...string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" && len(defaultValue) > 0 {
|
|
return defaultValue[0]
|
|
}
|
|
return value
|
|
}
|
|
|
|
func (e *envType) 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 (e *envType) 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
|
|
}
|