modelRT/config/config.go

92 lines
3.3 KiB
Go
Raw Normal View History

// Package config define config struct of model runtime service
package config
import (
"fmt"
"github.com/spf13/viper"
)
2024-11-28 11:46:40 +08:00
// 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"`
2024-11-28 11:46:40 +08:00
}
// 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"`
2024-11-28 11:46:40 +08:00
}
// ModelRTConfig define config stuct of model runtime server
type ModelRTConfig struct {
2024-11-28 11:46:40 +08:00
BaseConfig `mapstructure:"base"`
PostgresConfig `mapstructure:"postgres"`
KafkaConfig `mapstructure:"kafka"`
LoggerConfig `mapstructure:"logger"`
AntsConfig `mapstructure:"ants"`
DataRTConfig `mapstructure:"dataRT"`
2024-11-28 11:46:40 +08:00
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 {
2024-11-28 11:46:40 +08:00
panic(fmt.Sprintf("can not find conifg file:%s\n", err.Error()))
}
panic(err)
}
if err := config.Unmarshal(&modelRTConfig); err != nil {
2024-11-28 11:46:40 +08:00
panic(fmt.Sprintf("unmarshal modelRT config failed:%s\n", err.Error()))
}
// init postgres db uri
modelRTConfig.PostgresDBURI = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", modelRTConfig.PostgresConfig.Host, modelRTConfig.PostgresConfig.Port, modelRTConfig.PostgresConfig.User, modelRTConfig.PostgresConfig.Password, modelRTConfig.PostgresConfig.DataBase)
return modelRTConfig
}