93 lines
3.3 KiB
Go
93 lines
3.3 KiB
Go
// Package config define config struct of model runtime service
|
|
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
|
|
PollingConcurrentQuantity int `mapstructure:"polling_concurrent_quantity"` // polling real time data concurrent quantity
|
|
}
|
|
|
|
// DataRTConfig define config stuct of data runtime server api config
|
|
type DataRTConfig struct {
|
|
Host string `mapstructure:"host"`
|
|
Port int64 `mapstructure:"port"`
|
|
PollingAPI string `mapstructure:"polling_api"`
|
|
Method string `mapstructure:"polling_api_method"`
|
|
}
|
|
|
|
// 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"`
|
|
DataRTConfig `mapstructure:"dataRT"`
|
|
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)
|
|
}
|
|
|
|
modelRTconf := ModelRTConfig{}
|
|
if err := config.Unmarshal(&modelRTConfig); 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", modelRTconf.PostgresConfig.Host, modelRTconf.PostgresConfig.Port, modelRTconf.PostgresConfig.User, modelRTconf.PostgresConfig.Password, modelRTconf.PostgresConfig.DataBase)
|
|
|
|
return modelRTConfig
|
|
}
|