125 lines
2.6 KiB
Go
125 lines
2.6 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var MyConfig *Config
|
|
|
|
type Config struct {
|
|
Nats NatsConfig
|
|
Subscription SubscriptionConfig
|
|
Publisher PublisherConfig
|
|
Timeouts TimeoutsConfig
|
|
Hasura HasuraConfig
|
|
}
|
|
|
|
type NatsConfig struct {
|
|
Client string
|
|
URL string
|
|
Cluster string
|
|
}
|
|
|
|
type SubscriptionConfig struct {
|
|
Topic string `mapstructure:"topic"`
|
|
QueueGroup string `mapstructure:"queue_group"`
|
|
}
|
|
|
|
type PublisherConfig struct {
|
|
Topic string `mapstructure:"topic"`
|
|
}
|
|
|
|
type TimeoutsConfig struct {
|
|
Server time.Duration `mapstructure:"server"`
|
|
ReconnectWait time.Duration `mapstructure:"reconnect_wait"`
|
|
Close time.Duration `mapstructure:"close"`
|
|
AckWait time.Duration `mapstructure:"ack_wait"`
|
|
}
|
|
|
|
type BodyConfig struct {
|
|
Patterns []PatternConfig
|
|
}
|
|
|
|
type PatternConfig struct {
|
|
Pattern string
|
|
Comments string
|
|
Expression *regexp.Regexp
|
|
}
|
|
|
|
type HasuraConfig struct {
|
|
Endpoint string
|
|
Secret string
|
|
}
|
|
|
|
const (
|
|
EnvProd = "prod"
|
|
EnvDev = "dev"
|
|
EnvTest = "test"
|
|
)
|
|
|
|
func SetMyConfig(cfg *Config) {
|
|
MyConfig = cfg
|
|
}
|
|
|
|
func GetMyConfig() *Config {
|
|
if MyConfig == nil {
|
|
cfg, err := LoadConfig()
|
|
if err != nil {
|
|
fmt.Printf("error loading config: %v", err)
|
|
}
|
|
MyConfig = cfg
|
|
}
|
|
return MyConfig
|
|
}
|
|
|
|
// LoadConfig loads the configuration from a file
|
|
func LoadConfig() (*Config, error) {
|
|
// log := utils.Logger
|
|
env := os.Getenv("GO_ENV")
|
|
if env == "" {
|
|
env = "dev"
|
|
}
|
|
// log.Infof("Environment: %s", env)
|
|
|
|
viper.SetConfigType("toml")
|
|
viper.SetConfigName("config." + env)
|
|
viper.AddConfigPath("configs")
|
|
viper.SetEnvPrefix("tele")
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
// log.Errorf("error reading config file for environment '%s': %v", env, err)
|
|
return nil, fmt.Errorf("error reading config file for environment '%s': %w", env, err)
|
|
}
|
|
|
|
var config Config
|
|
if err := viper.Unmarshal(&config); err != nil {
|
|
// log.Errorf("unable to decode config into struct for environment '%s': %v", env, err)
|
|
return nil, fmt.Errorf("unable to decode config into struct for environment '%s': %w", env, err)
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
// ValidateConfig validates the loaded configuration
|
|
func ValidateConfig(cfg *Config) error {
|
|
// log := utils.Logger
|
|
|
|
if cfg.Nats.Client == "" {
|
|
return fmt.Errorf("nats client is required")
|
|
}
|
|
if cfg.Nats.URL == "" {
|
|
return fmt.Errorf("nats URL is required")
|
|
}
|
|
if cfg.Subscription.Topic == "" {
|
|
return fmt.Errorf("subscription topic is required")
|
|
}
|
|
// fmt.Println("config validation passed")
|
|
return nil
|
|
}
|