46 lines
721 B
Go
46 lines
721 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `mapstructure:"server"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int `mapstructure:"port"`
|
|
Host string `mapstructure:"host"`
|
|
}
|
|
|
|
var AppConfig Config
|
|
|
|
func LoadConfig() error {
|
|
env := os.Getenv("PROFILE")
|
|
var configName string
|
|
if env == "" {
|
|
configName = "config"
|
|
} else {
|
|
configName = fmt.Sprint("config.", env)
|
|
}
|
|
|
|
log.Println("Loading config: ", configName)
|
|
|
|
viper.SetConfigName(configName)
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := viper.Unmarshal(&AppConfig); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|