57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
|
|
// Package config define config struct of wave record project
|
||
|
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"wave_record/log"
|
||
|
|
|
||
|
|
"github.com/spf13/viper"
|
||
|
|
)
|
||
|
|
|
||
|
|
// WaveRecordConfig is designed for wave record config struct
|
||
|
|
type WaveRecordConfig struct {
|
||
|
|
MonitorDir string
|
||
|
|
ParseConcurrentQuantity int // parse comtrade file concurrent quantity
|
||
|
|
MongoDBURI string
|
||
|
|
MongoDBDataBase string
|
||
|
|
LCfg log.LogConfig // log config
|
||
|
|
}
|
||
|
|
|
||
|
|
// ComtradeFileConfig define config struct of parse comtrade data
|
||
|
|
type ComtradeFileConfig struct {
|
||
|
|
ConfigFilePath string
|
||
|
|
DatafilePath string
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReadAndInitConfig return wave record project config struct
|
||
|
|
func ReadAndInitConfig(configDir, configName, configType string) (waveRecordConfig WaveRecordConfig) {
|
||
|
|
config := viper.New()
|
||
|
|
config.AddConfigPath(configDir)
|
||
|
|
config.SetConfigName(configName)
|
||
|
|
config.SetConfigType(configType)
|
||
|
|
if err := config.ReadInConfig(); err != nil {
|
||
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||
|
|
panic("can not find conifg file")
|
||
|
|
}
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
waveRecordConfig.MonitorDir = config.GetString("comtrade_monitor_dir")
|
||
|
|
// init mongodb config from config.yaml
|
||
|
|
mongoDBHost := config.GetString("mongodb_host")
|
||
|
|
mongoDBPort := config.GetString("mongodb_port")
|
||
|
|
waveRecordConfig.MongoDBURI = strings.Join([]string{"mongodb://", mongoDBHost, ":", mongoDBPort}, "")
|
||
|
|
waveRecordConfig.MongoDBDataBase = config.GetString("mongodb_database")
|
||
|
|
// init zap log config from config.yaml
|
||
|
|
waveRecordConfig.LCfg.Level = config.GetString("log_level")
|
||
|
|
waveRecordConfig.LCfg.FileName = config.GetString("log_filename")
|
||
|
|
waveRecordConfig.LCfg.MaxSize = config.GetInt("log_maxsize")
|
||
|
|
waveRecordConfig.LCfg.MaxBackups = config.GetInt("log_maxbackups")
|
||
|
|
waveRecordConfig.LCfg.MaxAge = config.GetInt("log_maxage")
|
||
|
|
|
||
|
|
waveRecordConfig.ParseConcurrentQuantity = config.GetInt("parse_concurrent_quantity")
|
||
|
|
|
||
|
|
return waveRecordConfig
|
||
|
|
}
|