Skip to content

Commit 558b0ea

Browse files
author
Simon Prickett
authored
Merge pull request #1 from redislabs-training/feature/add-password-support
Add support for connecting to password protected Redis.
2 parents df9fbe0 + 9e7a485 commit 558b0ea

29 files changed

+83
-33
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ $ npm install
2727
The application uses a configuration file, `config.json` to specify the port that it listens
2828
on plus some logging parameters and how it connects to a database.
2929

30-
The supplied `config.json` file is already set up to use Redis on localhost port 6379. Change these values if your Redis instance is on another host or port.
30+
The supplied `config.json` file is already set up to use Redis on localhost port 6379. Change these values if your Redis instance is on another host or port, or requires a password to connect.
3131

3232
```
3333
{
@@ -40,6 +40,7 @@ The supplied `config.json` file is already set up to use Redis on localhost port
4040
"redis": {
4141
"host": "localhost",
4242
"port": 6379,
43+
"password": null,
4344
"keyPrefix": "ru102js"
4445
}
4546
}

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"redis": {
99
"host": "localhost",
1010
"port": 6379,
11+
"password": null,
1112
"keyPrefix": "ru102js"
1213
}
1314
}

src/daos/impl/redis/redis_client.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ bluebird.promisifyAll(redis);
1111

1212
// Create a client and connect to Redis using configuration
1313
// from config.json.
14-
const client = redis.createClient({
14+
const clientConfig = {
1515
host: config.get('dataStores.redis.host'),
1616
port: config.get('dataStores.redis.port'),
17-
});
17+
};
18+
19+
if (config.get('dataStores.redis.password')) {
20+
clientConfig.password = config.get('dataStores.redis.password');
21+
}
22+
23+
const client = redis.createClient(clientConfig);
1824

1925
// This is a catch all basic error handler.
2026
client.on('error', error => console.log(error));

src/examples/blocking_commands.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const startConsumer = async () => {
1414
const consumerClient = redis.createClient({
1515
port: 6379,
1616
host: '127.0.0.1',
17+
// password: 'password',
1718
});
1819

1920
while (!done) {
@@ -48,6 +49,7 @@ const startProducer = async () => {
4849
const producerClient = redis.createClient({
4950
port: 6379,
5051
host: '127.0.0.1',
52+
// password: 'password',
5153
});
5254

5355
// Run the producer at one second intervals.

src/examples/error_handling_async_await.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const replyErrorExamples = async () => {
88
const client = redis.createClient({
99
port: 6379,
1010
host: '127.0.0.1',
11+
// password: 'password',
1112
});
1213

1314
const key = 'replyErrorTest';

src/examples/error_handling_connection_error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const connectionErrorExample = async () => {
99
const client = redis.createClient({
1010
port: 6379,
1111
host: '127.0.0.1',
12+
// password: 'password',
1213
retry_strategy: (options) => {
1314
if (options.attempt > 5) {
1415
return new Error('Retry attempts exhausted.');

src/examples/error_handling_listener.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const redis = require('redis');
33
const client = redis.createClient({
44
port: 6379,
55
host: '127.0.0.1',
6+
// password: 'password',
67
});
78

89
// Add top level error listner.

src/examples/event_examples.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const redis = require('redis');
33
const client = redis.createClient({
44
port: 6379,
55
host: '127.0.0.1',
6+
// password: 'password',
67
});
78

89
client.on('ready', () => console.log('Redis client is ready!'));

src/examples/hello_async_await.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const runApplication = async () => {
1313
const client = redis.createClient({
1414
host: 'localhost',
1515
port: 6379,
16+
// password: 'password',
1617
});
1718

1819
// Run a Redis command.

src/examples/hello_basic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const redis = require('redis');
44
const client = redis.createClient({
55
host: 'localhost',
66
port: 6379,
7+
// password: 'password',
78
});
89

910
// Run a Redis command, receive response in callback.

0 commit comments

Comments
 (0)