imbytecat 8b7e7a3f85
All checks were successful
Template Cleanup / Template Cleanup (push) Has been skipped
convert to template
2024-09-04 11:36:03 +08:00

49 lines
873 B
Go

package config
import (
"fmt"
"github.com/spf13/viper"
"log"
"os"
"zabbixagent2plugintemplate/constant"
)
type config struct {
App appConfig `mapstructure:"app"`
}
type appConfig struct {
Port int `mapstructure:"port"`
Host string `mapstructure:"host"`
URL string `mapstructure:"url"` // web server url
}
var Config config
func LoadConfig() error {
env := os.Getenv("PROFILE")
var configName string
if env == "" {
configName = "config"
} else {
configName = fmt.Sprint("config.", env)
}
viper.SetConfigName(configName)
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath(fmt.Sprintf("/etc/%s", constant.AppName))
if err := viper.ReadInConfig(); err != nil {
return err
}
log.Println("Loading config file:", viper.ConfigFileUsed())
if err := viper.Unmarshal(&Config); err != nil {
return err
}
return nil
}