|
| 1 | +package apis |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/asaskevich/govalidator" |
| 9 | + "github.com/spf13/viper" |
| 10 | +) |
| 11 | + |
| 12 | +type PublisherType string |
| 13 | + |
| 14 | +const ( |
| 15 | + PublisherTypeNats PublisherType = "nats" |
| 16 | + PublisherTypeKafka PublisherType = "kafka" |
| 17 | + PublisherTypeRabbitMQ PublisherType = "rabbitmq" |
| 18 | + PublisherTypeGooglePubSub PublisherType = "google_pubsub" |
| 19 | +) |
| 20 | + |
| 21 | +// Config for pgoutbox. |
| 22 | +type Config struct { |
| 23 | + Listener *ListenerCfg `valid:"required" json:"listener" mapstructure:"listener"` |
| 24 | + Database *DatabaseCfg `valid:"required" json:"database" mapstructure:"database"` |
| 25 | + Publisher *PublisherCfg `valid:"required" json:"publisher" mapstructure:"publisher"` |
| 26 | + Logger *Logger `valid:"required" json:"logger" mapstructure:"logger"` |
| 27 | +} |
| 28 | + |
| 29 | +// ListenerCfg path of the listener config. |
| 30 | +type ListenerCfg struct { |
| 31 | + SlotName string `valid:"required" json:"slotName" mapstructure:"slotName"` |
| 32 | + ServerPort int `json:"serverPort" mapstructure:"serverPort"` |
| 33 | + AckTimeout time.Duration `json:"ackTimeout" mapstructure:"ackTimeout"` |
| 34 | + RefreshConnection time.Duration `json:"refreshConnection" valid:"required" mapstructure:"refreshConnection"` |
| 35 | + HeartbeatInterval time.Duration `json:"heartbeatInterval" valid:"required" mapstructure:"heartbeatInterval"` |
| 36 | + Filter FilterStruct `json:"filter" mapstructure:"filter"` |
| 37 | + TopicsMap map[string]string `json:"topicsMap" mapstructure:"topicsMap"` |
| 38 | +} |
| 39 | + |
| 40 | +// PublisherCfg represent configuration for any publisher types. |
| 41 | +type PublisherCfg struct { |
| 42 | + Type PublisherType `valid:"required" json:"type" mapstructure:"type"` |
| 43 | + Address string `valid:"required" json:"address" mapstructure:"address"` |
| 44 | + NatsCredPath string `valid:"required" json:"natsCredPath" mapstructure:"natsCredPath"` |
| 45 | + Topic string `valid:"required" json:"topic" mapstructure:"topic"` |
| 46 | + TopicPrefix string `json:"topicPrefix" mapstructure:"topicPrefix"` |
| 47 | + EnableTLS bool `json:"enableTLS" mapstructure:"enableTlS"` |
| 48 | + ClientCert string `json:"clientCert" mapstructure:"clientCert"` |
| 49 | + ClientKey string `json:"clientKey" mapstructure:"clientKey"` |
| 50 | + CACert string `json:"CACert" mapstructure:"caCert"` |
| 51 | + PubSubProjectID string `json:"pubSubProjectID" mapstructure:"pubSubProductId"` |
| 52 | +} |
| 53 | + |
| 54 | +// DatabaseCfg path of the PostgreSQL DB config. |
| 55 | +type DatabaseCfg struct { |
| 56 | + Host string `valid:"required" json:"host" mapstructure:"host"` |
| 57 | + Port uint16 `valid:"required" json:"port" mapstructure:"port"` |
| 58 | + Name string `valid:"required" json:"name" mapstructure:"name"` |
| 59 | + User string `valid:"required" json:"user" mapstructure:"user"` |
| 60 | + Password string `valid:"required" json:"password" mapstructure:"password"` |
| 61 | + Debug bool `json:"debug" mapstructure:"debug"` |
| 62 | +} |
| 63 | + |
| 64 | +// FilterStruct incoming WAL message filter. |
| 65 | +type FilterStruct struct { |
| 66 | + Tables map[string][]string `json:"tables" yaml:"tables" mapstructure:"tables"` |
| 67 | +} |
| 68 | + |
| 69 | +// Validate config data. |
| 70 | +func (c Config) Validate() error { |
| 71 | + _, err := govalidator.ValidateStruct(c) |
| 72 | + return err |
| 73 | +} |
| 74 | + |
| 75 | +// InitConfig load config from file. |
| 76 | +func InitConfig(path string) (*Config, error) { |
| 77 | + const envPrefix = "WAL" |
| 78 | + |
| 79 | + var conf Config |
| 80 | + |
| 81 | + vp := viper.New() |
| 82 | + |
| 83 | + vp.SetEnvPrefix(envPrefix) |
| 84 | + vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) |
| 85 | + vp.AutomaticEnv() |
| 86 | + vp.SetConfigFile(path) |
| 87 | + |
| 88 | + if err := vp.ReadInConfig(); err != nil { |
| 89 | + return nil, fmt.Errorf("error reading config: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + if err := vp.Unmarshal(&conf); err != nil { |
| 93 | + return nil, fmt.Errorf("unable to decode into config struct: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return &conf, nil |
| 97 | +} |
0 commit comments