| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2013-12-02 15:45:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -05: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'), | 
					
						
							| 
									
										
										
										
											2014-03-01 15:45:43 -05:00
										 |  |  | 		path = require('path'), | 
					
						
							| 
									
										
										
										
											2014-07-02 14:07:08 -04:00
										 |  |  | 		session = require('express-session'), | 
					
						
							| 
									
										
										
										
											2013-12-09 14:16:04 -05:00
										 |  |  | 		utils = require('./../../public/src/utils.js'), | 
					
						
							|  |  |  | 		redis, | 
					
						
							|  |  |  | 		connectRedis, | 
					
						
							| 
									
										
										
										
											2014-02-21 00:37:03 -05:00
										 |  |  | 		reds, | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		redisClient, | 
					
						
							|  |  |  | 		postSearch, | 
					
						
							|  |  |  | 		topicSearch; | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							|  |  |  | 			hidden: true | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05: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
										 |  |  | 			reds = require('reds'); | 
					
						
							|  |  |  | 		} catch (err) { | 
					
						
							|  |  |  | 			winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message); | 
					
						
							|  |  |  | 			process.exit(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-22 01:39:57 -04:00
										 |  |  | 		var redis_socket_or_host = nconf.get('redis:host'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05: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 */ | 
					
						
							|  |  |  | 			redisClient = redis.createClient(nconf.get('redis:host')); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Else, connect over tcp/ip */ | 
					
						
							|  |  |  | 			redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		if (nconf.get('redis:password')) { | 
					
						
							|  |  |  | 			redisClient.auth(nconf.get('redis:password')); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			winston.warn('You have no redis password setup!'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-23 19:17:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		redisClient.on('error', function (err) { | 
					
						
							| 
									
										
										
										
											2014-04-28 18:55:09 -04:00
										 |  |  | 			winston.error(err.stack); | 
					
						
							| 
									
										
										
										
											2014-05-28 20:40:40 -04:00
										 |  |  | 			process.exit(1); | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											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-03-01 16:59:04 -05:00
										 |  |  | 		reds.createClient = function () { | 
					
						
							|  |  |  | 			return reds.client || (reds.client = redisClient); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2014-02-21 00:37:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 15:29:01 -04:00
										 |  |  | 		module.postSearch = reds.createSearch('nodebbpostsearch'); | 
					
						
							|  |  |  | 		module.topicSearch = reds.createSearch('nodebbtopicsearch'); | 
					
						
							| 
									
										
										
										
											2014-02-21 00:37:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		var db = parseInt(nconf.get('redis:database'), 10); | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 		if (db) { | 
					
						
							|  |  |  | 			redisClient.select(db, function(error) { | 
					
						
							|  |  |  | 				if(error) { | 
					
						
							|  |  |  | 					winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message); | 
					
						
							|  |  |  | 					process.exit(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-02 15:46:25 -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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 15:29:01 -04: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
										 |  |  | 
 | 
					
						
							|  |  |  | 	module.close = function() { | 
					
						
							|  |  |  | 		redisClient.quit(); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-22 01:39:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 |