@@ -10,6 +10,7 @@ import (
1010var Server server
1111var ServerWs serverWs
1212var ServerGRPC serverGRPC
13+ var ServerMQTT serverMQTT
1314
1415type server struct {
1516 DedupEnabled bool
@@ -37,6 +38,39 @@ type serverGRPC struct {
3738 TLSPublicKey string
3839}
3940
41+ // serverMQTT represents the complete configuration for an MQTT server setup.
42+ // It includes authentication, Consul configuration, and consumer-specific settings.
43+ type serverMQTT struct {
44+ ConsulConfig consul // Configuration related to Consul service discovery
45+ AuthConfig auth // MQTT authentication credentials
46+ ConsumerConfig consumer // Consumer behavior and connection settings
47+ ConnGroup string // Connection group name used for identifying MQTT client group
48+ }
49+
50+ // auth defines MQTT authentication credentials.
51+ type auth struct {
52+ Username string // Username for authenticating with the MQTT broker
53+ Password string // Password for authenticating with the MQTT broker
54+ }
55+
56+ // consul holds configuration details for connecting and interacting with a Consul agent.
57+ type consul struct {
58+ Address string // Address of the Consul agent (e.g., localhost:8500)
59+ HealthOnly bool // When true, only healthy service instances are used
60+ KVKey string // Key in Consul KV store for retrieving MQTT Broker Address
61+ WaitTime time.Duration // Maximum wait time for Consul blocking queries
62+ }
63+
64+ // consumer contains configuration parameters controlling MQTT message consumption.
65+ type consumer struct {
66+ RetryIntervalInSec time.Duration // Time interval (in seconds) before retrying connection
67+ LogLevel string // Log verbosity level (e.g., "info", "debug", "error")
68+ WriteTimeoutInSec time.Duration // Timeout duration (in seconds) for write operations
69+ PoolSize int // Number of concurrent consumers to consume from MQTT topic
70+ TopicFormat string // Format or pattern for subscribing to MQTT topics (e.g., "share/raccoon/{service}")
71+ KeepAlive time.Duration // Amount of time that the client should wait before sending a PING request to the broker
72+ }
73+
4074func serverConfigLoader () {
4175 viper .SetDefault ("SERVER_BATCH_DEDUP_IN_CONNECTION_ENABLED" , "false" )
4276 Server = server {
@@ -85,3 +119,41 @@ func serverGRPCConfigLoader() {
85119 TLSPublicKey : util .MustGetString ("SERVER_GRPC_TLS_PUBLIC_KEY" ),
86120 }
87121}
122+
123+ func serverMQTTConfigLoader () {
124+ viper .SetDefault ("SERVER_MQTT_CONSUL_ADDRESS" , "consul:8081" )
125+ viper .SetDefault ("SERVER_MQTT_CONSUL_KV_KEY" , "kv/path" )
126+ viper .SetDefault ("SERVER_MQTT_CONSUL_HEALTH_ONLY" , true )
127+ viper .SetDefault ("SERVER_MQTT_CONSUL_WAIT_TIME" , 300 )
128+ viper .SetDefault ("SERVER_MQTT_AUTH_USERNAME" , "test" )
129+ viper .SetDefault ("SERVER_MQTT_AUTH_PASSWORD" , "pass" )
130+ viper .SetDefault ("SERVER_MQTT_CONSUMER_RETRY_INTERVAL_IN_SEC" , 1 )
131+ viper .SetDefault ("SERVER_MQTT_CONSUMER_WRITE_TIMEOUT_IN_SEC" , 1 )
132+ viper .SetDefault ("SERVER_MQTT_CONSUMER_KEEP_ALIVE_IN_SEC" , 45 )
133+ viper .SetDefault ("SERVER_MQTT_CONSUMER_LOG_LEVEL" , "warn" )
134+ viper .SetDefault ("SERVER_MQTT_CONSUMER_POOL_SIZE" , 1 )
135+ viper .SetDefault ("SERVER_MQTT_CONSUMER_TOPIC_FORMAT" , "default-topic" )
136+ viper .SetDefault ("SERVER_MQTT_CONNECTION_GROUP" , "default" )
137+
138+ ServerMQTT = serverMQTT {
139+ ConsulConfig : consul {
140+ Address : util .MustGetString ("SERVER_MQTT_CONSUL_ADDRESS" ),
141+ HealthOnly : util .MustGetBool ("SERVER_MQTT_CONSUL_HEALTH_ONLY" ),
142+ KVKey : util .MustGetString ("SERVER_MQTT_CONSUL_KV_KEY" ),
143+ WaitTime : util .MustGetDuration ("SERVER_MQTT_CONSUL_WAIT_TIME" , time .Second ),
144+ },
145+ AuthConfig : auth {
146+ Username : util .MustGetString ("SERVER_MQTT_AUTH_USERNAME" ),
147+ Password : util .MustGetString ("SERVER_MQTT_AUTH_PASSWORD" ),
148+ },
149+ ConsumerConfig : consumer {
150+ RetryIntervalInSec : util .MustGetDuration ("SERVER_MQTT_CONSUMER_RETRY_INTERVAL_IN_SEC" , time .Second ),
151+ LogLevel : util .MustGetString ("SERVER_MQTT_CONSUMER_LOG_LEVEL" ),
152+ WriteTimeoutInSec : util .MustGetDuration ("SERVER_MQTT_CONSUMER_WRITE_TIMEOUT_IN_SEC" , time .Second ),
153+ PoolSize : util .MustGetInt ("SERVER_MQTT_CONSUMER_POOL_SIZE" ),
154+ TopicFormat : util .MustGetString ("SERVER_MQTT_CONSUMER_TOPIC_FORMAT" ),
155+ KeepAlive : util .MustGetDuration ("SERVER_MQTT_CONSUMER_KEEP_ALIVE_IN_SEC" , time .Second ),
156+ },
157+ ConnGroup : util .MustGetString ("SERVER_MQTT_CONNECTION_GROUP" ),
158+ }
159+ }
0 commit comments