Skip to content

Commit fcf9228

Browse files
authored
db.redis: add optional password support with edis.connect(password: some_pass) (#25371)
1 parent 9c4dfd3 commit fcf9228

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

vlib/db/redis/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import db.redis
2020
2121
fn main() {
2222
// Connect to Redis
23+
// Uncomment passwod line if authentication is needed
2324
mut db := redis.connect(redis.Config{
2425
host: 'localhost'
26+
// password: 'your_password'
2527
port: 6379
2628
})!
2729

vlib/db/redis/redis.v

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,49 @@ mut:
3333
@[params]
3434
pub struct Config {
3535
pub mut:
36-
host string = '127.0.0.1' // Redis server host
37-
port u16 = 6379 // Redis server port
38-
version int = 2 // RESP protocol version (default: v2)
36+
host string = '127.0.0.1' // Redis server host
37+
port u16 = 6379 // Redis server port
38+
password string // Redis server password (optional)
39+
version int = 2 // RESP protocol version (default: v2)
3940
}
4041

4142
// connect establishes a connection to a Redis server
4243
pub fn connect(config Config) !DB {
43-
conn := net.dial_tcp('${config.host}:${config.port}')!
44-
return DB{
45-
conn: conn
44+
mut db := DB{
45+
conn: net.dial_tcp('${config.host}:${config.port}')!
4646
version: config.version
4747
cmd_buf: []u8{cap: cmd_buf_pre_allocate_len}
4848
resp_buf: []u8{cap: resp_buf_pre_allocate_len}
4949
}
50+
51+
// Authenticate if password is provided
52+
if config.password.len > 0 {
53+
db.auth(config.password)!
54+
}
55+
56+
return db
5057
}
5158

5259
// close terminates the connection to Redis server
5360
pub fn (mut db DB) close() ! {
5461
db.conn.close()!
5562
}
5663

64+
// auth sends an AUTH command to the server with the given password.
65+
pub fn (mut db DB) auth(password string) ! {
66+
resp := db.cmd('AUTH', password)!
67+
match resp {
68+
string {
69+
if resp != 'OK' {
70+
return error('Authentication failed: ${resp}')
71+
}
72+
}
73+
else {
74+
return error('Authentication failed: unexpected response type')
75+
}
76+
}
77+
}
78+
5779
// ping sends a PING command to verify server responsiveness
5880
pub fn (mut db DB) ping() !string {
5981
db.conn.write_string('*1\r\n$4\r\nPING\r\n')!

vlib/db/redis/redis_test.v

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// vtest build: started_redis?
2-
import db.redis
2+
import os
33
import time
4+
import db.redis
5+
6+
const redis_password = os.getenv('VREDIS_PASSWORD')
47

58
fn test_redis() {
6-
mut db := redis.connect() or { panic(err) }
9+
mut db := redis.connect(password: redis_password)!
710
assert db.ping()! == 'PONG'
811

912
// delete all keys first
@@ -57,7 +60,7 @@ fn test_redis() {
5760
}
5861

5962
fn test_redis_pipeline() {
60-
mut db := redis.connect() or { panic(err) }
63+
mut db := redis.connect(password: redis_password)!
6164
assert db.ping()! == 'PONG'
6265

6366
// start pipleline mode

0 commit comments

Comments
 (0)