modelRT/config/config.go

60 lines
2.2 KiB
Go
Raw Normal View History

// Package config define config struct of wave record project
package config
import (
"fmt"
"time"
"modelRT/constant"
"modelRT/log"
"github.com/spf13/viper"
)
// ModelRTConfig define config stuct of model runtime server
type ModelRTConfig struct {
GridID int64
ZoneID int64
StationID int64
ParseConcurrentQuantity int // parse comtrade file concurrent quantity
PostgresDBURI string
LCfg log.CutLogConfig // log config
}
// ReadAndInitConfig return wave record project config struct
func ReadAndInitConfig(configDir, configName, configType string) (modelRTConfig ModelRTConfig) {
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)
}
// init postgresdb config from config.yaml
postgresDBHost := config.GetString("postgresdb_host")
postgresDBPort := config.GetInt("postgresdb_port")
postgresDBUser := config.GetString("postgresdb_user")
postgresDBPassword := config.GetString("postgresdb_password")
postgresDBDataBase := config.GetString("postgresdb_database")
modelRTConfig.PostgresDBURI = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", postgresDBHost, postgresDBPort, postgresDBUser, postgresDBPassword, postgresDBDataBase)
// init zap log config from config.yaml
modelRTConfig.LCfg.Mode = config.GetString("log_mode")
modelRTConfig.LCfg.Level = config.GetString("log_level")
modelRTConfig.LCfg.FileName = fmt.Sprintf(config.GetString("log_filepath"), time.Now().Format(constant.LogTimeFormate))
modelRTConfig.LCfg.MaxSize = config.GetInt("log_maxsize")
modelRTConfig.LCfg.MaxBackups = config.GetInt("log_maxbackups")
modelRTConfig.LCfg.MaxAge = config.GetInt("log_maxage")
// init model base config from config.yaml
modelRTConfig.GridID = config.GetInt64("grid_id")
modelRTConfig.GridID = config.GetInt64("zone_id")
modelRTConfig.GridID = config.GetInt64("station_id")
modelRTConfig.ParseConcurrentQuantity = config.GetInt("parse_concurrent_quantity")
return modelRTConfig
}