X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=941f7d5574021b5f91b38bc3034276d13fbab30a;hb=a84b8fa5cf6e4cafb841af3db9bdfcc9531c09a4;hp=0acb9ff0e4438864041a450e7b5e8afcd8403864;hpb=fd4484f19eae8b0a0c30d5d30e98880c8708516a;p=oweals%2Fpeertube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 0acb9ff0e..941f7d557 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -24,10 +24,7 @@ class Redis { if (this.initialized === true) return this.initialized = true - this.client = createClient({ - host: CONFIG.REDIS.HOSTNAME, - port: CONFIG.REDIS.PORT - }) + this.client = createClient(Redis.getRedisClient()) this.client.on('error', err => { logger.error('Error in Redis client.', { err }) @@ -41,6 +38,16 @@ class Redis { this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-' } + static getRedisClient () { + return Object.assign({}, + (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, + (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, + (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ? + { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } : + { path: CONFIG.REDIS.SOCKET } + ) + } + async setResetPasswordVerificationString (userId: number) { const generatedString = await generateRandomString(32) @@ -68,23 +75,26 @@ class Redis { } setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { - const cached: CachedRoute = { - body: body.toString(), - contentType, - statusCode: statusCode.toString() - } + const cached: CachedRoute = Object.assign({}, { + body: body.toString() + }, + (contentType) ? { contentType } : null, + (statusCode) ? { statusCode: statusCode.toString() } : null + ) return this.setObject(this.buildCachedRouteKey(req), cached, lifetime) } - listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) { - return new Promise((res, rej) => { - this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => { - if (err) return rej(err) + generateResetPasswordKey (userId: number) { + return 'reset-password-' + userId + } - return res(values) - }) - }) + buildViewKey (ip: string, videoUUID: string) { + return videoUUID + '-' + ip + } + + buildCachedRouteKey (req: express.Request) { + return req.method + '-' + req.originalUrl } private getValue (key: string) { @@ -145,18 +155,6 @@ class Redis { }) } - private generateResetPasswordKey (userId: number) { - return 'reset-password-' + userId - } - - private buildViewKey (ip: string, videoUUID: string) { - return videoUUID + '-' + ip - } - - private buildCachedRouteKey (req: express.Request) { - return req.method + '-' + req.originalUrl - } - static get Instance () { return this.instance || (this.instance = new this()) }