59 lines
966 B
Go
59 lines
966 B
Go
package config
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
App struct {
|
|
Port int
|
|
}
|
|
Redis struct {
|
|
Host string
|
|
Port int
|
|
DB int
|
|
Password string
|
|
}
|
|
Hydra struct {
|
|
Host string
|
|
Password string
|
|
}
|
|
DB struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
Dbname string
|
|
}
|
|
}
|
|
|
|
func NewConfig() Config {
|
|
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)
|
|
}
|
|
return config
|
|
}
|