56 lines
993 B
Go
56 lines
993 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type config struct {
|
|
Server serverConfig `mapstructure:"server"`
|
|
App appConfig `mapstructure:"app"`
|
|
Integrations integrationConfig `mapstructure:"integrations"`
|
|
}
|
|
|
|
type serverConfig struct {
|
|
Port int `mapstructure:"port"`
|
|
Host string `mapstructure:"host"`
|
|
}
|
|
|
|
type appConfig struct {
|
|
URL string `mapstructure:"url"`
|
|
}
|
|
|
|
type integrationConfig struct {
|
|
ZabbixAgent IntegrationConfig `mapstructure:"zabbix_agent"`
|
|
}
|
|
|
|
var Conf 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(&Conf); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|