eventRT/config/config.go

93 lines
3.2 KiB
Go
Raw Permalink Normal View History

2026-01-29 16:54:00 +08:00
// Package config define config struct of model runtime service
package config
import (
"fmt"
2026-01-30 17:43:16 +08:00
"net/url"
2026-01-29 16:54:00 +08:00
"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 {
2026-01-30 17:43:16 +08:00
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
2026-01-29 16:54:00 +08:00
Database string `mapstructure:"database"`
2026-01-30 17:43:16 +08:00
AuthDB string `mapstructure:"auth_db"`
Port int `mapstructure:"port"`
2026-01-29 16:54:00 +08:00
Timeout int `mapstructure:"timeout"`
}
type RabbitMQConfig struct {
2026-02-06 17:55:30 +08:00
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"`
2026-01-29 16:54:00 +08:00
}
// 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"`
2026-01-30 17:43:16 +08:00
RabbitMQConfig `mapstructure:"rabbitMQ"`
MongoDBURI string
2026-01-29 16:54:00 +08:00
}
// 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()))
}
2026-01-30 17:43:16 +08:00
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)
2026-01-29 16:54:00 +08:00
return eventRTConfig
}