// Package config define config struct of model runtime service package config import ( "fmt" "github.com/spf13/viper" ) // LoggerConfig define config struct 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"` Compress bool `mapstructure:"compress"` } // MongoDBConfig define config struct of mongoDB config type MongoDBConfig struct { URI string `mapstructure:"uri"` Database string `mapstructure:"database"` Timeout int `mapstructure:"timeout"` } // RabbitMQConfig define config struct of RabbitMQ config type RabbitMQConfig struct { URL string `mapstructure:"url"` User string `mapstructure:"user"` Password string `mapstructure:"password"` Host string `mapstructure:"host"` Port int `mapstructure:"port"` } // ServiceConfig define config struct of service config type ServiceConfig struct { ServiceAddr string `mapstructure:"service_addr"` ServiceName string `mapstructure:"service_name"` SecretKey string `mapstructure:"secret_key"` DeployEnv string `mapstructure:"deploy_env"` } // EventRTConfig define config struct of model runtime server type EventRTConfig struct { ServiceConfig `mapstructure:"service"` LoggerConfig `mapstructure:"logger"` MongoDBConfig `mapstructure:"mongodb"` RabbitMQConfig `mapstructure:"rabbitmq"` } // ReadAndInitConfig return eventRT project config struct func ReadAndInitConfig(configDir, configName, configType string) (eventRTConfig EventRTConfig) { 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) } if err := config.Unmarshal(&eventRTConfig); err != nil { panic(fmt.Sprintf("unmarshal eventRT config failed:%s\n", err.Error())) } // init postgres db uri // eventRTConfig.PostgresDBURI = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", eventRTConfig.PostgresConfig.Host, eventRTConfig.PostgresConfig.Port, eventRTConfig.PostgresConfig.User, eventRTConfig.PostgresConfig.Password, eventRTConfig.PostgresConfig.DataBase) return eventRTConfig }