eventRT/config/config.go

93 lines
3.2 KiB
Go

// Package config define config struct of model runtime service
package config
import (
"fmt"
"net/url"
"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 {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
AuthDB string `mapstructure:"auth_db"`
Port int `mapstructure:"port"`
Timeout int `mapstructure:"timeout"`
}
type RabbitMQConfig struct {
CACertPath string `mapstructure:"ca_cert_path"`
ClientKeyPath string `mapstructure:"client_key_path"`
ClientKeyPassword string `mapstructure:"client_key_password"`
ClientCertPath string `mapstructure:"client_cert_path"`
InsecureSkipVerify bool `mapstructure:"insecure_skip_verify"`
ServerName string `mapstructure:"server_name"`
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"`
MongoDBURI string
}
// 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()))
}
if eventRTConfig.MongoDBConfig.Timeout <= 0 {
eventRTConfig.MongoDBConfig.Timeout = 10
}
if eventRTConfig.MongoDBConfig.AuthDB == "" {
eventRTConfig.MongoDBConfig.AuthDB = "admin"
}
// init mongoDB uri
user := url.QueryEscape(eventRTConfig.MongoDBConfig.User)
password := url.QueryEscape(eventRTConfig.MongoDBConfig.Password)
eventRTConfig.MongoDBURI = fmt.Sprintf("mongodb://%s:%s@%s:%d/?authSource=%s", user, password, eventRTConfig.MongoDBConfig.Host, eventRTConfig.MongoDBConfig.Port, eventRTConfig.MongoDBConfig.AuthDB)
return eventRTConfig
}