49 lines
858 B
Go
49 lines
858 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"onvif-agent/constant"
|
|
"os"
|
|
)
|
|
|
|
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
|
|
}
|