Skip to content

Commit d095a03

Browse files
committed
feat: add support for Redis authentication and TLS configuration
- Add Redis configuration options to README.md - Add `with_tls`, `username`, and `password` fields to Redis queue configuration in config.go - Update config.go to load new Redis configuration options using viper - Add tests for new Redis configuration options in config_test.go - Add `username`, `password`, and `with_tls` fields to Redis queue configuration in config.yml - Update main.go to use new Redis configuration options with conditional TLS setup Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 46d11a5 commit d095a03

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ queue:
183183
group: gorush
184184
consumer: gorush
185185
stream_name: gorush
186+
redis:
187+
addr: 127.0.0.1:6379
188+
group: gorush
189+
consumer: gorush
190+
stream_name: gorush
191+
with_tls: false
192+
username: ""
193+
password: ""
186194

187195
ios:
188196
enabled: false

config/config.go

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ queue:
8282
group: gorush
8383
consumer: gorush
8484
stream_name: gorush
85+
with_tls: false
86+
username: ""
87+
password: ""
8588
8689
ios:
8790
enabled: false
@@ -255,9 +258,12 @@ type SectionNATS struct {
255258
// SectionRedisQueue is sub section of config.
256259
type SectionRedisQueue struct {
257260
Addr string `yaml:"addr"`
261+
Username string `yaml:"username"`
262+
Password string `yaml:"password"`
258263
StreamName string `yaml:"stream_name"`
259264
Group string `yaml:"group"`
260265
Consumer string `yaml:"consumer"`
266+
WithTLS bool `yaml:"with_tls"`
261267
}
262268

263269
// SectionRedis is sub section of config.
@@ -424,6 +430,9 @@ func LoadConf(confPath ...string) (*ConfYaml, error) {
424430
conf.Queue.Redis.StreamName = viper.GetString("queue.redis.stream_name")
425431
conf.Queue.Redis.Group = viper.GetString("queue.redis.group")
426432
conf.Queue.Redis.Consumer = viper.GetString("queue.redis.consumer")
433+
conf.Queue.Redis.WithTLS = viper.GetBool("queue.redis.with_tls")
434+
conf.Queue.Redis.Username = viper.GetString("queue.redis.username")
435+
conf.Queue.Redis.Password = viper.GetString("queue.redis.password")
427436

428437
// Stat Engine
429438
conf.Stat.Engine = viper.GetString("stat.engine")

config/config_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
113113
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Queue.Redis.StreamName)
114114
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Queue.Redis.Group)
115115
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Queue.Redis.Consumer)
116+
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Queue.Redis.Username)
117+
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Queue.Redis.Password)
118+
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Queue.Redis.WithTLS)
116119

117120
// log
118121
assert.Equal(suite.T(), "string", suite.ConfGorushDefault.Log.Format)

config/testdata/config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ queue:
7070
group: gorush
7171
consumer: gorush
7272
stream_name: gorush
73+
username: ""
74+
password: ""
75+
with_tls: false
7376

7477
ios:
7578
enabled: false

main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,22 @@ func main() {
328328
nats.WithLogger(logx.QueueLogger()),
329329
)
330330
case core.Redis:
331-
w = redisdb.NewWorker(
331+
opts := []redisdb.Option{
332332
redisdb.WithAddr(cfg.Queue.Redis.Addr),
333+
redisdb.WithUsername(cfg.Queue.Redis.Username),
334+
redisdb.WithPassword(cfg.Queue.Redis.Password),
333335
redisdb.WithStreamName(cfg.Queue.Redis.StreamName),
334336
redisdb.WithGroup(cfg.Queue.Redis.Group),
335337
redisdb.WithConsumer(cfg.Queue.Redis.Consumer),
336338
redisdb.WithMaxLength(cfg.Core.QueueNum),
337339
redisdb.WithRunFunc(notify.Run(cfg)),
338340
redisdb.WithLogger(logx.QueueLogger()),
341+
}
342+
if cfg.Queue.Redis.WithTLS {
343+
opts = append(opts, redisdb.WithTLS())
344+
}
345+
w = redisdb.NewWorker(
346+
opts...,
339347
)
340348
default:
341349
logx.LogError.Fatalf("we don't support queue engine: %s", cfg.Queue.Engine)

0 commit comments

Comments
 (0)