X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=06a34006012f5e0e6ae96a2e56ff23e16d1af5f9;hb=40e87e9ecc54e3513fb586928330a7855eb192c6;hp=1e7c0a8211ebe791b1a1e27cdca0f2e2d7e87407;hpb=4195cd2bc5046d4cdf1c677c27cd41f427d7a13a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 1e7c0a821..06a340060 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -2,7 +2,7 @@ import * as express from 'express' import { createClient, RedisClient } from 'redis' import { logger } from '../helpers/logger' import { generateRandomString } from '../helpers/utils' -import { CONFIG, FEEDS, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' +import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' type CachedRoute = { body: string, @@ -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) @@ -67,24 +74,26 @@ class Redis { return cached as CachedRoute } - setCachedRoute (req: express.Request, body: any, contentType?: string, statusCode?: number) { + setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { const cached: CachedRoute = { body: body.toString(), contentType, statusCode: statusCode.toString() } - return this.setObject(this.buildCachedRouteKey(req), cached, FEEDS.CACHE_LIFETIME) + 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 +154,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()) }