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' ) ,
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
utils = require ( './../../public/src/utils.js' ) ,
redis ,
connectRedis ,
2015-03-14 23:00:24 -04:00
redisSearch ,
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' ,
2015-02-25 22:27:19 +01:00
hidden : true ,
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
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 ) ;
2015-03-14 23:00:24 -04:00
redisSearch = require ( 'redisearch' ) ;
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
2015-03-14 23:00:24 -04:00
module . postSearch = redisSearch . createSearch ( 'nodebbpostsearch' , redisClient ) ;
module . topicSearch = redisSearch . createSearch ( 'nodebbtopicsearch' , redisClient ) ;
2014-02-21 00:37:03 -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
2014-11-24 13:44:59 -05:00
module . connect = function ( options ) {
2014-09-23 17:08:30 -04:00
var redis _socket _or _host = nconf . get ( 'redis:host' ) ,
2014-09-30 23:57:03 -04:00
cxn , dbIdx ;
2014-09-23 17:08:30 -04:00
2014-11-24 13:44:59 -05:00
options = options || { } ;
2014-10-08 12:22:39 -04:00
if ( ! redis ) {
redis = require ( 'redis' ) ;
}
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
}
2014-09-30 23:57:03 -04:00
dbIdx = parseInt ( nconf . get ( 'redis:database' ) , 10 ) ;
if ( dbIdx ) {
cxn . select ( dbIdx , function ( error ) {
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 ;
} ;
2015-06-05 13:33:58 -04:00
module . checkCompatibility = function ( callback ) {
// Redis requires v2.8.9
module . info ( module . client , function ( err , info ) {
var err = semver . lt ( info . redis _version , '2.8.9' ) ? new Error ( 'Your Redis version is not new enough to support NodeBB, please upgrade Redis to v2.8.9 or higher.' ) : null ;
if ( err ) {
err . stacktrace = false ;
}
callback ( err ) ;
} ) ;
} ;
2014-04-14 13:51:45 -04:00
module . close = function ( ) {
redisClient . quit ( ) ;
} ;
2014-04-22 01:39:57 -04:00
2015-05-08 15:36:06 -04:00
module . info = function ( cxn , callback ) {
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