54 lines
1.0 KiB
Go
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()
|
|
}
|