Skip to content

Commit 9e7a485

Browse files
author
Simon Prickett
committed
Adds support for connecting to password-protected Redis.
1 parent bccc665 commit 9e7a485

20 files changed

+73
-36
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));

tests/basic.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
const redis = require('redis');
2-
const bluebird = require('bluebird');
1+
const config = require('better-config');
32

4-
bluebird.promisifyAll(redis);
3+
config.set('../config.json');
54

6-
const client = redis.createClient({
7-
host: 'localhost',
8-
port: 6379,
9-
// password: 'password',
10-
});
5+
const redis = require('../src/daos/impl/redis/redis_client');
6+
7+
const client = redis.getClient();
118

129
const testSuiteName = 'Basic';
1310

tests/capacity_dao_redis_impl.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const config = require('better-config');
2+
3+
config.set('../config.json');
4+
25
const redis = require('../src/daos/impl/redis/redis_client');
36
const redisCapacityDAO = require('../src/daos/impl/redis/capacity_dao_redis_impl');
47
const keyGenerator = require('../src/daos/impl/redis/redis_key_generator');
@@ -7,7 +10,6 @@ const testSuiteName = 'capacity_dao_redis_impl';
710

811
const testKeyPrefix = `test:${testSuiteName}`;
912

10-
config.set('../config.json');
1113
keyGenerator.setPrefix(testKeyPrefix);
1214
const client = redis.getClient();
1315

tests/compare_and_update_script.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const config = require('better-config');
2+
3+
config.set('../config.json');
4+
15
const redis = require('../src/daos/impl/redis/redis_client');
26
const compareAndUpdateScript = require('../src/daos/impl/redis/scripts/compare_and_update_script');
37

tests/data_types.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
const redis = require('redis');
2-
const bluebird = require('bluebird');
1+
const config = require('better-config');
32

4-
bluebird.promisifyAll(redis);
3+
config.set('../config.json');
54

6-
const client = redis.createClient({
7-
host: 'localhost',
8-
port: 6379,
9-
// password: 'password',
10-
});
5+
const redis = require('../src/daos/impl/redis/redis_client');
6+
7+
const client = redis.getClient();
118

129
const testSuiteName = 'data_types';
1310
const testKeyName = `${testSuiteName}:test`;

tests/feed_dao_redis_impl.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const config = require('better-config');
2+
3+
config.set('../config.json');
4+
25
const redis = require('../src/daos/impl/redis/redis_client');
36
const redisFeedDAO = require('../src/daos/impl/redis/feed_dao_redis_impl');
47
const keyGenerator = require('../src/daos/impl/redis/redis_key_generator');
@@ -7,7 +10,6 @@ const testSuiteName = 'feed_dao_redis_impl';
710

811
const testKeyPrefix = `test:${testSuiteName}`;
912

10-
config.set('../config.json');
1113
keyGenerator.setPrefix(testKeyPrefix);
1214
const client = redis.getClient();
1315

tests/hello.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
const redis = require('redis');
2-
const bluebird = require('bluebird');
1+
const config = require('better-config');
32

4-
bluebird.promisifyAll(redis);
3+
config.set('../config.json');
54

6-
const client = redis.createClient({
7-
host: 'localhost',
8-
port: 6379,
9-
// password: 'password',
10-
});
5+
const redis = require('../src/daos/impl/redis/redis_client');
6+
7+
const client = redis.getClient();
118

129
const testSuiteName = 'Hello';
1310
const testKey = 'hello';

tests/lua.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const config = require('better-config');
2+
3+
config.set('../config.json');
4+
15
const redis = require('../src/daos/impl/redis/redis_client');
26
const updateIfLowestScript = require('../src/daos/impl/redis/scripts/update_if_lowest_script');
37

0 commit comments

Comments
 (0)