Files
shields/lib/redis-token-persistence.js
Paul Melnikow a16d436602 Optionally persist [Github] tokens in Redis (#1939)
This is a fairly simple addition of a Redis-backed TokenPersistence. When GithubConstellation is initialized, it will create a FsTokenPersistence or a RedisTokenPersistence based on configuration. Have added tests of the Redis backend as an integration test, and ensured the server starts up correctly when a `REDIS_URL` is configured.

Ref: #1848
2018-08-19 10:27:23 -04:00

53 lines
1.1 KiB
JavaScript

'use strict'
const redis = require('redis')
const { promisify } = require('util')
const log = require('./log')
const githubAuth = require('./github-auth')
const TokenPersistence = require('./token-persistence')
class RedisTokenPersistence extends TokenPersistence {
constructor({ url, key }) {
super()
this.url = url
this.key = key
}
async initialize() {
this.client = redis.createClient(this.url)
this.client.on('error', e => {
log.error(e)
})
const lrange = promisify(this.client.lrange).bind(this.client)
let tokens
try {
tokens = await lrange(this.key, 0, -1)
} catch (e) {
log.error(e)
}
tokens.forEach(tokenString => {
githubAuth.addGithubToken(tokenString)
})
}
async stop() {
const quit = promisify(this.client.quit).bind(this.client)
await quit()
}
async onTokenAdded(token) {
const rpush = promisify(this.client.rpush).bind(this.client)
await rpush(this.key, token)
}
async onTokenRemoved(token) {
const lrem = promisify(this.client.lrem).bind(this.client)
await lrem(this.key, 0, token)
}
}
module.exports = RedisTokenPersistence