iwilldie/src/helper/loadenv.go
janic f70b58c70d
All checks were successful
continuous-integration/drone/push Build is passing
Adding basic webserver and loading default values
2023-03-11 22:20:08 +01:00

54 lines
1.0 KiB
Go

package helper
import (
"log"
"os"
"github.com/joho/godotenv"
"github.com/spf13/viper"
)
func Loadenv() {
// Load environment variables from .env file if it exists
err := godotenv.Load()
if err != nil {
log.Println(err)
}
// Set prefix for environment variables
viper.AutomaticEnv()
// Set default values
viper.SetDefault("MAX_TTL", 300)
viper.SetDefault("MIN_TTL", 10)
viper.SetDefault("HARDFAIL", false)
viper.SetDefault("HARDFAIL_DELAY", 10)
dotenv_exists := FileExists(".env")
if dotenv_exists {
// Read configuration from file
viper.SetConfigType("dotenv")
viper.SetConfigName(".env")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
if err != nil {
log.Println(err)
}
} else {
log.Println("No Environment file found")
}
MAX_TTL = viper.GetInt("MAX_TTL")
MIN_TTL = viper.GetInt("MIN_TTL")
HARDFAIL = viper.GetBool("HARDFAIL")
HARDFAIL_DELAY = viper.GetInt("HARDFAIL_DELAY")
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}