-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backend, server): move 'getUniversalRedis' to helper and exp…
…ort it, export Redis (#9)
- Loading branch information
1 parent
b293b85
commit 3efcf16
Showing
4 changed files
with
63 additions
and
52 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { readFileSync } from 'fs' | ||
import Redis from 'ioredis' | ||
import RedisMock from 'ioredis-mock' | ||
|
||
export { Redis, RedisMock } | ||
|
||
export function getRedis ({ enableRedis = true, redisOpts, redisUrl, keyPrefix }) { | ||
if (enableRedis) { | ||
if (typeof redisOpts === 'string') { | ||
redisOpts = JSON.parse(redisOpts) | ||
let tls = {} | ||
|
||
if (redisOpts.key) { | ||
tls = { | ||
key: readFileSync(redisOpts.key), | ||
cert: readFileSync(redisOpts.cert), | ||
ca: readFileSync(redisOpts.ca) | ||
} | ||
} | ||
|
||
const options = { | ||
sentinels: redisOpts.sentinels, | ||
sslPort: redisOpts.ssl_port || '6380', | ||
tls, | ||
name: 'mymaster', | ||
db: redisOpts.db || 0, | ||
password: redisOpts.password | ||
} | ||
|
||
_maybeAddKeyPrefixToOptions(options, keyPrefix) | ||
|
||
return new Redis(options) | ||
} else if (redisUrl) { | ||
const options = {} | ||
_maybeAddKeyPrefixToOptions(options, keyPrefix) | ||
|
||
return new Redis(redisUrl, options) | ||
} | ||
} | ||
|
||
const options = {} | ||
_maybeAddKeyPrefixToOptions(options, keyPrefix) | ||
|
||
return new RedisMock(options) | ||
} | ||
|
||
function _maybeAddKeyPrefixToOptions (options, keyPrefix) { | ||
if (keyPrefix) options.keyPrefix = keyPrefix | ||
} |
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