Skip to content
Snippets Groups Projects
Commit 419dae67 authored by Tobias Meisel's avatar Tobias Meisel Committed by Steffen Schulze
Browse files

envconfig

parent 0fcd3b58
No related branches found
No related tags found
No related merge requests found
......@@ -25,10 +25,14 @@ type CloudEventProvider interface {
ReplyCtx(ctx context.Context, responseFunc func(ctx context.Context, event event.Event) (*event.Event, error)) error
}
// New initializes a CloudEventProviderClient using the passed Config. In contrast to NewClient, it
// does not rely on a configuration other than the one provided.
func New(conf Config, conType ConnectionType, topic string) (*CloudEventProviderClient, error) {
return newClient(conf, conType, topic)
}
// NewClient initializes a CloudEventProviderClient. It uses viper.Viper as the config library and
// might make assumptions about your config.yaml
func NewClient(connectionType ConnectionType, topic string) (*CloudEventProviderClient, error) {
cloudEventProviderViper = viper.New()
......@@ -40,6 +44,7 @@ func NewClient(connectionType ConnectionType, topic string) (*CloudEventProvider
return newClient(*config, connectionType, topic)
}
// NewEvent initializes an event.Event ready to be submitted
func NewEvent(eventSource string, eventType string, data json.RawMessage) (event.Event, error) {
newEvent := cloudevents.NewEvent()
newEvent.SetID(uuid.NewString())
......
......@@ -22,9 +22,9 @@ type protocolConfig interface {
}
type NatsConfig struct {
Url string `mapstructure:"url"`
QueueGroup string `mapstructure:"queueGroup,omitempty"`
TimeoutInSec time.Duration `mapstructure:"timeoutInSec,omitempty"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
QueueGroup string `mapstructure:"queueGroup,omitempty" envconfig:"QUEUE_GROUP"`
TimeoutInSec time.Duration `mapstructure:"timeoutInSec,omitempty" envconfig:"REQUEST_TIMEOUT"`
}
func (c NatsConfig) validate() error {
......@@ -36,10 +36,10 @@ func (c NatsConfig) validate() error {
}
type NatsJetstreamConfig struct {
Url string `mapstructure:"url"`
QueueGroup string `mapstructure:"queueGroup,omitempty"`
StreamType string `mapstructure:"streamType"`
TimeoutInSec time.Duration `mapstructure:"timeoutInSec,omitempty"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
QueueGroup string `mapstructure:"queueGroup,omitempty" envconfig:"QUEUE_GROUP"`
StreamType string `mapstructure:"streamType" envconfig:"STREAM_TYPE"`
TimeoutInSec time.Duration `mapstructure:"timeoutInSec,omitempty" envconfig:"REQUEST_TIMEOUT"`
}
func (c NatsJetstreamConfig) validate() error {
......@@ -55,9 +55,9 @@ func (c NatsJetstreamConfig) validate() error {
}
type KafkaConfig struct {
Url string `mapstructure:"url"`
GroupId string `mapstructure:"groupId,omitempty"`
ClientId string `mapstructure:"clientId"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
GroupId string `mapstructure:"groupId,omitempty" envconfig:"GROUP_ID"`
ClientId string `mapstructure:"clientId" envconfig:"CLIENT_ID"`
}
func (c KafkaConfig) validate() error {
......@@ -73,8 +73,8 @@ func (c KafkaConfig) validate() error {
}
type MqttConfig struct {
Url string `mapstructure:"url"`
ClientId string `mapstructure:"clientId"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
ClientId string `mapstructure:"clientId" envconfig:"CLIENT_ID"`
}
func (c MqttConfig) validate() error {
......@@ -90,7 +90,7 @@ func (c MqttConfig) validate() error {
}
type AmqpConfig struct {
Url string `mapstructure:"url"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
}
func (c AmqpConfig) validate() error {
......@@ -102,9 +102,9 @@ func (c AmqpConfig) validate() error {
}
type HttpConfig struct {
Url string `mapstructure:"url"`
Port int `mapstructure:"port"`
Path string `mapstructure:"path"`
Url string `mapstructure:"url" envconfig:"URL" default:"127.0.0.1"`
Port int `mapstructure:"port" envconfig:"PORT" default:"8080"`
Path string `mapstructure:"path" envconfig:"PATH"`
}
func (c HttpConfig) validate() error {
......@@ -125,14 +125,14 @@ func (c HttpConfig) validate() error {
type cloudEventProviderConfiguration struct {
Messaging struct {
Protocol ProtocolType `mapstructure:"protocol"`
Nats NatsConfig `mapstructure:"nats"`
NatsJetstream NatsJetstreamConfig `mapstructure:"natsJetstream"`
Kafka KafkaConfig `mapstructure:"kafka"`
Mqtt MqttConfig `mapstructure:"mqtt"`
Amqp AmqpConfig `mapstructure:"amqp"`
Http HttpConfig `mapstructure:"http"`
} `mapstructure:"messaging"`
Protocol ProtocolType `mapstructure:"protocol" envconfig:"PROTOCOL_TYPE"`
Nats NatsConfig `mapstructure:"nats" envconfig:"NATS"`
NatsJetstream NatsJetstreamConfig `mapstructure:"natsJetstream" envconfig:"JETSTREAM"`
Kafka KafkaConfig `mapstructure:"kafka" envconfig:"KAFKA"`
Mqtt MqttConfig `mapstructure:"mqtt" envconfig:"MQTT"`
Amqp AmqpConfig `mapstructure:"amqp" envconfig:"AMQP"`
Http HttpConfig `mapstructure:"http" envconfig:"HTTP"`
} `mapstructure:"messaging" envconfig:"MESSAGING"`
}
func loadConfig() (*Config, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment