| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2013-12-02 15:45:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | (function (module) { | 
					
						
							| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 	var winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -05:00
										 |  |  | 		nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2015-06-05 13:33:58 -04:00
										 |  |  | 		semver = require('semver'), | 
					
						
							| 
									
										
										
										
											2014-07-02 14:07:08 -04:00
										 |  |  | 		session = require('express-session'), | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 		redis, | 
					
						
							|  |  |  | 		connectRedis, | 
					
						
							| 
									
										
										
										
											2015-12-11 10:57:13 +02:00
										 |  |  | 		redisClient; | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 	module.questions = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'redis:host', | 
					
						
							|  |  |  | 			description: 'Host IP or address of your Redis instance', | 
					
						
							|  |  |  | 			'default': nconf.get('redis:host') || '127.0.0.1' | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'redis:port', | 
					
						
							|  |  |  | 			description: 'Host port of your Redis instance', | 
					
						
							|  |  |  | 			'default': nconf.get('redis:port') || 6379 | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: 'redis:password', | 
					
						
							| 
									
										
										
										
											2014-04-14 20:33:02 -04:00
										 |  |  | 			description: 'Password of your Redis database', | 
					
						
							| 
									
										
										
										
											2015-02-25 22:27:19 +01:00
										 |  |  | 			hidden: true, | 
					
						
							| 
									
										
										
										
											2016-08-04 20:58:04 +03:00
										 |  |  | 			default: nconf.get('redis:password') || '', | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			before: function (value) { value = value || nconf.get('redis:password') || ''; return value; } | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "redis:database", | 
					
						
							|  |  |  | 			description: "Which database to use (0..n)", | 
					
						
							|  |  |  | 			'default': nconf.get('redis:database') || 0 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]; | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.init = function (callback) { | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			redis = require('redis'); | 
					
						
							| 
									
										
										
										
											2014-07-02 15:15:06 -04:00
										 |  |  | 			connectRedis = require('connect-redis')(session); | 
					
						
							| 
									
										
										
										
											2014-04-14 13:41:33 -04:00
										 |  |  | 		} catch (err) { | 
					
						
							|  |  |  | 			winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message); | 
					
						
							|  |  |  | 			process.exit(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 		redisClient = module.connect(); | 
					
						
							| 
									
										
										
										
											2013-12-23 19:17:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		module.client = redisClient; | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		module.sessionStore = new connectRedis({ | 
					
						
							|  |  |  | 			client: redisClient, | 
					
						
							|  |  |  | 			ttl: 60 * 60 * 24 * 14 | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-12-02 22:33:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 15:29:01 -04:00
										 |  |  | 		require('./redis/main')(redisClient, module); | 
					
						
							|  |  |  | 		require('./redis/hash')(redisClient, module); | 
					
						
							|  |  |  | 		require('./redis/sets')(redisClient, module); | 
					
						
							|  |  |  | 		require('./redis/sorted')(redisClient, module); | 
					
						
							|  |  |  | 		require('./redis/list')(redisClient, module); | 
					
						
							| 
									
										
										
										
											2014-02-21 00:37:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 22:18:28 +03:00
										 |  |  | 		if (typeof callback === 'function') { | 
					
						
							| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2014-02-21 00:37:03 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-14 13:51:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.connect = function (options) { | 
					
						
							| 
									
										
										
										
											2016-09-29 14:31:24 +03:00
										 |  |  | 		var redis_socket_or_host = nconf.get('redis:host'); | 
					
						
							|  |  |  | 		var cxn; | 
					
						
							| 
									
										
										
										
											2014-11-24 13:44:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-08 12:22:39 -04:00
										 |  |  | 		if (!redis) { | 
					
						
							|  |  |  | 			redis = require('redis'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 14:31:24 +03:00
										 |  |  | 		options = options || {}; | 
					
						
							|  |  |  | 		if (nconf.get('redis:password')) { | 
					
						
							|  |  |  | 			options.auth_pass = nconf.get('redis:password'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 		if (redis_socket_or_host && redis_socket_or_host.indexOf('/') >= 0) { | 
					
						
							|  |  |  | 			/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */ | 
					
						
							| 
									
										
										
										
											2014-11-24 13:44:59 -05:00
										 |  |  | 			cxn = redis.createClient(nconf.get('redis:host'), options); | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Else, connect over tcp/ip */ | 
					
						
							| 
									
										
										
										
											2014-11-24 13:44:59 -05:00
										 |  |  | 			cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'), options); | 
					
						
							| 
									
										
										
										
											2014-09-25 17:13:09 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 		cxn.on('error', function (err) { | 
					
						
							|  |  |  | 			winston.error(err.stack); | 
					
						
							|  |  |  | 			process.exit(1); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:13:09 -04:00
										 |  |  | 		if (nconf.get('redis:password')) { | 
					
						
							|  |  |  | 			cxn.auth(nconf.get('redis:password')); | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 14:31:24 +03:00
										 |  |  | 		var dbIdx = parseInt(nconf.get('redis:database'), 10); | 
					
						
							| 
									
										
										
										
											2014-09-30 23:57:03 -04:00
										 |  |  | 		if (dbIdx) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			cxn.select(dbIdx, function (error) { | 
					
						
							| 
									
										
										
										
											2014-09-30 23:57:03 -04:00
										 |  |  | 				if(error) { | 
					
						
							|  |  |  | 					winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message); | 
					
						
							|  |  |  | 					process.exit(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-23 17:08:30 -04:00
										 |  |  | 		return cxn; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.checkCompatibility = function (callback) { | 
					
						
							|  |  |  | 		module.info(module.client, function (err, info) { | 
					
						
							| 
									
										
										
										
											2015-06-05 13:33:58 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2015-06-08 15:34:39 -04:00
										 |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (semver.lt(info.redis_version, '2.8.9')) { | 
					
						
							|  |  |  | 				err = new Error('Your Redis version is not new enough to support NodeBB, please upgrade Redis to v2.8.9 or higher.'); | 
					
						
							| 
									
										
										
										
											2015-06-05 13:33:58 -04:00
										 |  |  | 				err.stacktrace = false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-06-08 15:34:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 13:33:58 -04:00
										 |  |  | 			callback(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.close = function () { | 
					
						
							| 
									
										
										
										
											2014-04-14 13:51:45 -04:00
										 |  |  | 		redisClient.quit(); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-22 01:39:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.info = function (cxn, callback) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 		if (!cxn) { | 
					
						
							|  |  |  | 			return callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-08 15:36:06 -04:00
										 |  |  | 		cxn.info(function (err, data) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var lines = data.toString().split("\r\n").sort(); | 
					
						
							|  |  |  | 			var redisData = {}; | 
					
						
							|  |  |  | 			lines.forEach(function (line) { | 
					
						
							|  |  |  | 				var parts = line.split(':'); | 
					
						
							|  |  |  | 				if (parts[1]) { | 
					
						
							|  |  |  | 					redisData[parts[0]] = parts[1]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			redisData.raw = JSON.stringify(redisData, null, 4); | 
					
						
							|  |  |  | 			redisData.redis = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, redisData); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 15:15:21 -04:00
										 |  |  | 	module.helpers = module.helpers || {}; | 
					
						
							| 
									
										
										
										
											2014-04-15 12:02:36 -04:00
										 |  |  | 	module.helpers.redis = require('./redis/helpers'); | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -05:00
										 |  |  | }(exports)); | 
					
						
							| 
									
										
										
										
											2013-12-02 15:45:15 -05:00
										 |  |  | 
 |