-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into videos-frontend
- Loading branch information
Showing
4 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
const { Redis } = require('@senecacdot/satellite'); | ||
require('./config'); | ||
const Redis = require('ioredis'); | ||
const MockRedis = require('ioredis-mock'); | ||
const { logger } = require('../utils/logger'); | ||
const parseUrl = require('../utils/url-parser'); | ||
|
||
// If you need to set the Redis URL, do it in REDIS_URL | ||
const redisUrl = | ||
parseUrl(process.env.REDIS_URL, process.env.REDIS_PORT) || 'redis://127.0.0.1:6379'; | ||
|
||
// Set MOCK_REDIS=1 to mock, MOCK_REDIS= to use real redis | ||
const useMockRedis = process.env.MOCK_REDIS; | ||
|
||
// RedisConstructor is one of Redis or MockRedis | ||
const RedisConstructor = useMockRedis ? MockRedis : Redis; | ||
|
||
function createRedisClient() { | ||
try { | ||
const { port, host } = new URL(redisUrl); | ||
return new RedisConstructor(port, host, { password: process.env.REDIS_PASSWORD }); | ||
} catch (error) { | ||
const message = `Unable to parse port and host from "${redisUrl}"`; | ||
logger.error({ error }, message); | ||
throw new Error(message); | ||
} | ||
} | ||
|
||
// If using MockRedis, shim info() until https://github.com/stipsan/ioredis-mock/issues/841 ships | ||
if (useMockRedis && typeof MockRedis.prototype.info !== 'function') { | ||
logger.debug('Shimming MockRedis info() method'); | ||
MockRedis.prototype.info = () => Promise.resolve('redis_version:999.999.999'); | ||
} | ||
|
||
module.exports = { | ||
// If callers need to create a new redis instance, they'll use the ctor | ||
createRedisClient: Redis, | ||
createRedisClient, | ||
// Otherwise they can use this shared instance (most should use this) | ||
redis: Redis(), | ||
redis: createRedisClient(), | ||
}; |