// Package config define config struct of wave record project package config import ( "fmt" "github.com/spf13/viper" ) // BaseConfig define config stuct of base params config type BaseConfig struct { GridID int64 `mapstructure:"grid_id"` ZoneID int64 `mapstructure:"zone_id"` StationID int64 `mapstructure:"station_id"` } // KafkaConfig define config stuct of kafka config type KafkaConfig struct { Servers string `mapstructure:"Servers"` GroupID string `mapstructure:"group_id"` Topic string `mapstructure:"topic"` AutoOffsetReset string `mapstructure:"auto_offset_reset"` EnableAutoCommit string `mapstructure:"enable_auto_commit"` ReadMessageTimeDuration string `mapstructure:"read_message_time_duration"` } // PostgresConfig define config stuct of postgres config type PostgresConfig struct { Port int `mapstructure:"port"` Host string `mapstructure:"host"` DataBase string `mapstructure:"database"` User string `mapstructure:"user"` Password string `mapstructure:"password"` } // LoggerConfig define config stuct of zap logger config type LoggerConfig struct { Mode string `mapstructure:"mode"` Level string `mapstructure:"level"` FilePath string `mapstructure:"filepath"` MaxSize int `mapstructure:"maxsize"` MaxBackups int `mapstructure:"maxbackups"` MaxAge int `mapstructure:"maxage"` } // AntsConfig define config stuct of ants pool config type AntsConfig struct { ParseConcurrentQuantity int `mapstructure:"parse_concurrent_quantity"` // parse comtrade file concurrent quantity } // ModelRTConfig define config stuct of model runtime server type ModelRTConfig struct { BaseConfig `mapstructure:"base"` PostgresConfig `mapstructure:"postgres"` KafkaConfig `mapstructure:"kafka"` LoggerConfig `mapstructure:"logger"` AntsConfig `mapstructure:"ants"` PostgresDBURI string `mapstructure:"-"` } // 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(fmt.Sprintf("can not find conifg file:%s\n", err.Error())) } panic(err) } rtConfig := ModelRTConfig{} if err := config.Unmarshal(&rtConfig); err != nil { panic(fmt.Sprintf("unmarshal modelRT config failed:%s\n", err.Error())) } modelRTConfig.PostgresDBURI = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", rtConfig.Host, rtConfig.Port, rtConfig.User, rtConfig.Password, rtConfig.DataBase) return modelRTConfig }