-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
171 lines (144 loc) · 3.88 KB
/
config.go
File metadata and controls
171 lines (144 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package steadyrabbit
import (
"errors"
)
// QueueConfig -> queue info
type QueueConfig struct {
QueueName string
QueueDurable bool
QueueExclusive bool
QueueAutoDelete bool
QueueDeclare bool
}
// ExchangeConfig -> exchange info
type ExchangeConfig struct {
ExchangeName string
ExchangeType string
ExchangeDurable bool
ExchangeAutoDelete bool
ExchangeDeclare bool
}
// BindingConfig -> exchange and routing key
type BindingConfig struct {
RoutingKeys []string
Exchange *ExchangeConfig
}
// ConsumerConfig ...
type ConsumerConfig struct {
AutoAck bool
ConsumerTag string
QosPrefetchCount int
QosPrefetchSize int
QueueConfig *QueueConfig
Bindings []*BindingConfig
}
// PublisherConfig ...
type PublisherConfig struct {
Exchange *ExchangeConfig
PublishConfirmation bool
DeliveryMode uint8
Mandatory bool
Immediate bool
}
// Config steadyrabbit configuration
type Config struct {
URL string
RetryReconnectIntervalSec int
AppID string
UseTLS bool
SkipVerifyTLS bool
Publisher *PublisherConfig
Consumer *ConsumerConfig
}
// ValidatePublisherConfig validates config
func ValidatePublisherConfig(cnf *Config) error {
if cnf == nil {
return errors.New("Config can not be nil")
}
if cnf.URL == "" {
return errors.New("URL can not be empty")
}
if cnf.AppID == "" {
cnf.AppID = DefaultAppID
}
if cnf.RetryReconnectIntervalSec == 0 {
cnf.RetryReconnectIntervalSec = DefaultRetryReconnectIntervalSec
}
if cnf.Publisher == nil {
cnf.Publisher = &PublisherConfig{
DeliveryMode: DefaultDeliveryMode,
PublishConfirmation: DefaultPublisherConfirm,
Immediate: DefaultIsImmediatePublish,
Mandatory: DefaultIsMandatoryPublish,
}
}
if cnf.Publisher.Exchange == nil {
cnf.Publisher.Exchange = &ExchangeConfig{}
}
if cnf.Publisher.Exchange.ExchangeDeclare {
if cnf.Publisher.Exchange.ExchangeType == "" {
return errors.New("ExchangeType can not be empty if ExchangeDeclare set to true")
}
if cnf.Publisher.Exchange.ExchangeName == "" {
return errors.New("ExchangeName can not be empty if ExchangeDeclare set to true")
}
}
return nil
}
// ValidateConsumerConfig validates consumer config
func ValidateConsumerConfig(cnf *Config) error {
if cnf == nil {
return errors.New("Config can not be nil")
}
if cnf.URL == "" {
return errors.New("URL can not be empty")
}
if cnf.AppID == "" {
cnf.AppID = DefaultAppID
}
if cnf.RetryReconnectIntervalSec == 0 {
cnf.RetryReconnectIntervalSec = DefaultRetryReconnectIntervalSec
}
if cnf.Consumer == nil {
cnf.Consumer = &ConsumerConfig{
AutoAck: false,
ConsumerTag: DefaultConsumerTag,
QosPrefetchCount: 0,
QosPrefetchSize: 0,
Bindings: []*BindingConfig{},
}
}
if cnf.Consumer.ConsumerTag == "" {
cnf.Consumer.ConsumerTag = DefaultConsumerTag
}
if cnf.Consumer.QueueConfig == nil {
cnf.Consumer.QueueConfig = &QueueConfig{
QueueName: DefaultQueueName,
QueueDeclare: true,
QueueDurable: true,
}
}
if cnf.Consumer.QueueConfig.QueueName == "" {
cnf.Consumer.QueueConfig.QueueName = DefaultQueueName
}
// if len(cnf.Consumer.Bindings) == 0 {
// return errors.New("Consumer bindings can not be empty")
// }
for _, b := range cnf.Consumer.Bindings {
if b.Exchange == nil {
return errors.New("Exchange can not be empty")
}
if b.Exchange.ExchangeDeclare == true {
if b.Exchange.ExchangeType == "" {
return errors.New("ExchangeType can not be empty if ExchangeDeclare set to true")
}
if b.Exchange.ExchangeName == "" {
return errors.New("ExchangeName can not be empty if ExchangeDeclare set to true")
}
}
if len(b.RoutingKeys) == 0 {
return errors.New("Routing keys can not be empty")
}
}
return nil
}