package config import ( "github.com/joho/godotenv" "github.com/spf13/viper" "log" ) type Config struct { App struct { Port int } Redis struct { Host string Port int DB int Password string } Hydra struct { Host string Password string } } var Cfg *Config func Init() { err := godotenv.Load() if err != nil { log.Println("Error loading .env file") } viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(".") err = viper.ReadInConfig() if err != nil { log.Fatalf("Error reading config file, %s", err) } viper.AutomaticEnv() var config Config _ = viper.BindEnv("redis.password", "REDIS_PASSWORD") _ = viper.BindEnv("hydra.password", "HYDRA_PASSWORD") err = viper.Unmarshal(&config) if err != nil { log.Fatalf("Unable to decode config into struct, %v", err) } Cfg = &config }