file_monitoring/config/config.go

83 lines
2.2 KiB
Go

package config
import (
"git.hpds.cc/Component/logging"
"github.com/spf13/viper"
"os"
)
type Config struct {
MonitoringConf MonitoringDirectoryConfig
TransferConf TransferConfig
Log *logging.Logger
}
type MonitoringDirectoryConfig struct {
Path string `json:"path" yaml:"path" toml:"path"`
}
type TransferConfig struct {
Proto string `json:"proto" yaml:"proto" toml:"proto"`
Address string `json:"address" yaml:"address" toml:"address"`
Port int `json:"port" yaml:"port" toml:"port"`
}
var (
DefaultMonitoringDirectory = "./tmp/"
DefaultTransfer = TransferConfig{
Proto: "MQTT",
Address: "127.0.0.1",
Port: 6060,
}
)
// DefaultConfig return the default configuration.
// If config file is not provided, hpds_mqd will start with DefaultConfig.
func DefaultConfig() Config {
path, err := os.Getwd()
if err != nil {
panic(err)
}
vp := viper.New()
vp.AddConfigPath(".")
vp.AddConfigPath(path)
vp.AddConfigPath(path + "/cmd")
vp.AddConfigPath(path + "/config")
vp.SetConfigName("config")
vp.SetConfigType("yaml")
if err := vp.ReadInConfig(); err != nil {
panic(err)
}
DefaultMonitoringDirectory = vp.GetString("path")
DefaultTransfer = TransferConfig{
Proto: vp.GetString("transfer.proto"),
Address: vp.GetString("transfer.address"),
Port: vp.GetInt("transfer.port"),
}
c := Config{
MonitoringConf: MonitoringDirectoryConfig{Path: DefaultMonitoringDirectory},
TransferConf: DefaultTransfer,
Log: ParseLoggingConfig(vp),
}
return c
}
func ParseLoggingConfig(vp *viper.Viper) *logging.Logger {
logger := logging.NewLogger(
logging.SetPath(vp.GetString("log.path")),
logging.SetPrefix(vp.GetString("log.prefix")),
logging.SetDevelopment(vp.GetBool("log.development")),
logging.SetDebugFileSuffix(vp.GetString("log.debugFileSuffix")),
logging.SetWarnFileSuffix(vp.GetString("log.warnFileSuffix")),
logging.SetErrorFileSuffix(vp.GetString("log.errorFileSuffix")),
logging.SetInfoFileSuffix(vp.GetString("log.infoFileSuffix")),
logging.SetMaxAge(vp.GetInt("log.maxAge")),
logging.SetMaxBackups(vp.GetInt("log.maxBackups")),
logging.SetMaxSize(vp.GetInt("log.maxSize")),
logging.SetLevel(logging.LogLevel[vp.GetString("log.level")]),
)
return logger
}