78 lines
2.8 KiB
Go
78 lines
2.8 KiB
Go
// Package config define config struct of wave record project
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"wave_record/constant"
|
|
"wave_record/log"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// WaveRecordConfig define config of wave record struct
|
|
type WaveRecordConfig struct {
|
|
MonitorDir string
|
|
BackupDir string
|
|
ParseConcurrentQuantity int // parse comtrade file concurrent quantity
|
|
MongoDBURI string
|
|
MongoDBDataBase string
|
|
InfluxDBURL string
|
|
InfluxDBToken string
|
|
InfluxDBOrg string
|
|
InfluxDBBucket string
|
|
LCfg log.CutLogConfig // log config
|
|
}
|
|
|
|
// ComtradeDataStorageConfig define config struct of storage comtrade data
|
|
type ComtradeDataStorageConfig struct {
|
|
Ctx context.Context
|
|
DelChan chan string
|
|
DBName string
|
|
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")
|
|
waveRecordConfig.BackupDir = config.GetString("comtrade_backup_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 influxdb config from config.yaml
|
|
influxDBHost := config.GetString("influxdb_host")
|
|
influxDBPort := config.GetString("influxdb_port")
|
|
waveRecordConfig.InfluxDBURL = strings.Join([]string{"http://", influxDBHost, ":", influxDBPort}, "")
|
|
waveRecordConfig.InfluxDBToken = config.GetString("influxdb_token")
|
|
waveRecordConfig.InfluxDBOrg = config.GetString("influxdb_org")
|
|
waveRecordConfig.InfluxDBBucket = config.GetString("influxdb_bucket")
|
|
// init zap log config from config.yaml
|
|
waveRecordConfig.LCfg.Mode = config.GetString("log_mode")
|
|
waveRecordConfig.LCfg.Level = config.GetString("log_level")
|
|
waveRecordConfig.LCfg.FileName = fmt.Sprintf(config.GetString("log_filepath"), time.Now().Format(constant.LogTimeFormate))
|
|
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
|
|
}
|