Comparing version 0.0.1 to 0.0.2
@@ -14,4 +14,2 @@ /* | ||
var fs = require('fs'); | ||
var async = require('async'); | ||
var recursive = require('recursive-readdir'); | ||
var path = require('path'); | ||
@@ -117,5 +115,5 @@ var logger = require('./logger.js')('Config'); | ||
function _load(confPath, callback) { | ||
function _load(confPath) { | ||
function _loadByFile(confPath, callback){ | ||
function _loadByFile(confPath){ | ||
//load js or json as config | ||
@@ -126,68 +124,32 @@ var ext = path.extname(confPath); | ||
_.extend(config, require(confPath)); | ||
callback && callback(null); | ||
} else if (ext === '.json') { | ||
logger.trace('load config from ' +confPath); | ||
fs.readFile(confPath, function (err, content) { | ||
if (err){ | ||
logger.fatal('config file [' + confPath + '] read failed '); | ||
callback && callback(err); | ||
return; | ||
} | ||
_.extend(config, JSON.parse(content.toString())); | ||
callback && callback(null); | ||
}); | ||
}else{ | ||
callback && callback(null, config); | ||
var content = fs.readFileSync(confPath); | ||
_.extend(config, JSON.parse(content.toString())); | ||
} | ||
} | ||
function _loadByFolder(confPath, callback){ | ||
//recursively get files in folder | ||
recursive(confPath, function (err, files) { | ||
if (err){ | ||
logger.fatal('config folder [' + confPath + '] recursive failed'); | ||
callback && callback(err); | ||
return; | ||
} | ||
//load config | ||
async.map(files, _loadByFile, function(err){ | ||
if (err){ | ||
callback && callback(err); | ||
return; | ||
} | ||
callback && callback(null); | ||
}); | ||
}); | ||
function _loadByFolder(confPath){ | ||
var files = util.readdirSync(confPath); | ||
files.map(_loadByFile); | ||
} | ||
fs.stat(confPath, function (err, stats) { | ||
if (err){ | ||
logger.fatal('config path [' + confPath + '] stat failed '); | ||
callback && callback(err); | ||
return; | ||
} | ||
if (stats.isFile()) { | ||
_loadByFile(confPath, callback); | ||
} else if (stats.isDirectory()) { | ||
_loadByFolder(confPath, callback); | ||
} | ||
}); | ||
var stats = fs.statSync(confPath); | ||
if (stats.isFile()) { | ||
_loadByFile(confPath); | ||
} else if (stats.isDirectory()) { | ||
_loadByFolder(confPath); | ||
} | ||
} | ||
function load(confPath, callback){ | ||
function load(confPath){ | ||
confPath = path.normalize(confPath); | ||
_load(confPath, function(err){ | ||
if (err){ | ||
logger.fatal('config [' + confPath + '] load failed'); | ||
callback && callback(err); | ||
return; | ||
} | ||
try{ | ||
config = parse(config); | ||
}catch(e){ | ||
logger.fatal('config [' + confPath + '] parse error'); | ||
throw e; | ||
} | ||
callback && callback(null, config); | ||
}); | ||
try{ | ||
_load(confPath); | ||
config = parse(config); | ||
}catch(err){ | ||
logger.fatal('config [' + confPath + '] load failed'); | ||
throw err; | ||
} | ||
return config; | ||
} | ||
@@ -194,0 +156,0 @@ |
@@ -203,3 +203,3 @@ /* | ||
RAL.defaultOptions = { | ||
var defaultOptions = { | ||
confDir : null, | ||
@@ -214,30 +214,14 @@ extDir : [__dirname + path.sep + '/ext'], | ||
RAL.appendExtPath = function(path){ | ||
defaultOptions.extDir.push(path); | ||
}; | ||
RAL.init = function(options, callback){ | ||
options = ralUtil.merge(RAL.defaultOptions, options); | ||
RAL.init = function(options){ | ||
options = ralUtil.merge(defaultOptions, options); | ||
ctx.currentIDC = options.currentIDC; | ||
loggerGen.options = options.logger; | ||
var loadExtensionTask = []; | ||
if (options.extDir){ | ||
options.extDir.forEach(function(dir){ | ||
loadExtensionTask.push(async.apply(RalModule.load, dir)); | ||
}); | ||
} | ||
async.parallel(loadExtensionTask, function(err){ | ||
if (err){ | ||
logger.fatal('yog-ral load extension failed' + err.message); | ||
callback && callback(err); | ||
return; | ||
} | ||
config.load(options.confDir, function(err){ | ||
if (err){ | ||
logger.fatal('yog-ral load config fail' + err.message); | ||
callback && callback(err); | ||
} | ||
callback && callback(); | ||
}); | ||
}); | ||
options.extDir.map(RalModule.load); | ||
config.load(options.confDir); | ||
}; | ||
module.exports = RAL; |
@@ -12,4 +12,3 @@ /* | ||
var logger = require('./logger.js')('RalModule'); | ||
var recursive = require('recursive-readdir'); | ||
var async = require('async'); | ||
var ralUtil = require('./util.js'); | ||
var path = require('path'); | ||
@@ -33,3 +32,3 @@ | ||
RalModule.load = function(pathOrModule, callback){ | ||
RalModule.load = function(pathOrModule){ | ||
@@ -61,17 +60,8 @@ function loadFile(filePath){ | ||
if (_.isString(pathOrModule)){ | ||
recursive(pathOrModule, function (err, files) { | ||
if (err){ | ||
logger.fatal('ext folder [' + pathOrModule + '] recursive failed'); | ||
callback && callback(err); | ||
return; | ||
} | ||
files.map(loadFile); | ||
callback && callback(null); | ||
}); | ||
var files = ralUtil.readdirSync(pathOrModule); | ||
files.map(loadFile); | ||
}else if (pathOrModule.getCategory && pathOrModule.getName){ | ||
loadModule(pathOrModule); | ||
callback && callback(null); | ||
}else{ | ||
logger.trace('[' + pathOrModule + '] is skiped since not RalModule'); | ||
callback && callback(null); | ||
} | ||
@@ -78,0 +68,0 @@ }; |
@@ -10,2 +10,3 @@ /* | ||
var _ = require('underscore'); | ||
var fs = require('fs'); | ||
var util = module.exports; | ||
@@ -30,2 +31,18 @@ | ||
return content.join(' ') | ||
}; | ||
}; | ||
util.readdirSync = function(dir) { | ||
var results = []; | ||
var list = fs.readdirSync(dir); | ||
list.forEach(function(file) { | ||
file = dir + '/' + file; | ||
var stat = fs.statSync(file); | ||
if (stat && stat.isDirectory()) { | ||
results = results.concat(util.readdirSync(file)); | ||
} | ||
else { | ||
results.push(file) | ||
} | ||
}); | ||
return results | ||
}; |
{ | ||
"name": "node-ral", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "a rpc client for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,39 +40,31 @@ /* | ||
it('get server by all idc', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
context.reqIDCServers.should.eql(conf.bookService.server); | ||
done(); | ||
}); | ||
it('get server by all idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
context.reqIDCServers.should.eql(conf.bookService.server); | ||
}); | ||
it('get server by tc idc', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
context.reqIDCServers.should.have.length(1); | ||
context.reqIDCServers[0].idc.should.equal('tc'); | ||
context.crossIDCServers.should.have.length(1); | ||
context.crossIDCServers[0].idc.should.equal('st'); | ||
done(); | ||
}); | ||
it('get server by tc idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
context.reqIDCServers.should.have.length(1); | ||
context.reqIDCServers[0].idc.should.equal('tc'); | ||
context.crossIDCServers.should.have.length(1); | ||
context.crossIDCServers[0].idc.should.equal('st'); | ||
}); | ||
it('server conf without idc can use for all idc', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService2', conf.bookService2); | ||
context.reqIDCServers.should.have.length(2); | ||
context.reqIDCServers.should.eql(conf.bookService2.server); | ||
done(); | ||
}); | ||
it('server conf without idc can use for all idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService2', conf.bookService2); | ||
context.reqIDCServers.should.have.length(2); | ||
context.reqIDCServers.should.eql(conf.bookService2.server); | ||
}); | ||
it('service conf with hybird mode can use all server', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
ctx.currentIDC = 'st'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
context.reqIDCServers.should.eql(conf.bookService3.server); | ||
done(); | ||
}); | ||
it('service conf with hybird mode can use all server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
ctx.currentIDC = 'st'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
context.reqIDCServers.should.eql(conf.bookService3.server); | ||
}); | ||
@@ -97,42 +89,36 @@ }); | ||
it('get server by random', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
return 0.1; | ||
}; | ||
var server = balance.fetchServer(context); | ||
Math.random = SourceRandom; | ||
server.should.be.eql(conf.bookService3.server[0]); | ||
done(); | ||
}); | ||
it('get server by random', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
return 0.1; | ||
}; | ||
var server = balance.fetchServer(context); | ||
Math.random = SourceRandom; | ||
server.should.be.eql(conf.bookService3.server[0]); | ||
}); | ||
it('get server by random for real random', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RandomBalance(); | ||
var server = balance.fetchServer(context); | ||
conf.bookService3.server.should.be.containEql(server); | ||
done(); | ||
}); | ||
it('get server by random for real random', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RandomBalance(); | ||
var server = balance.fetchServer(context); | ||
conf.bookService3.server.should.be.containEql(server); | ||
}); | ||
it('direct use single server', function(done) { | ||
config.load(__dirname + path.sep + './config/singleserver_config.js', function(err, conf){ | ||
ctx.currentIDC = 'st'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
throw new Error(); | ||
}; | ||
(function(){balance.fetchServer(context);}).should.not.throwError(); | ||
var server = balance.fetchServer(context); | ||
Math.random = SourceRandom; | ||
server.should.be.eql(conf.bookService.server[0]); | ||
done(); | ||
}); | ||
it('direct use single server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/singleserver_config.js'); | ||
ctx.currentIDC = 'st'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
throw new Error(); | ||
}; | ||
(function(){balance.fetchServer(context);}).should.not.throwError(); | ||
var server = balance.fetchServer(context); | ||
Math.random = SourceRandom; | ||
server.should.be.eql(conf.bookService.server[0]); | ||
}); | ||
@@ -157,27 +143,23 @@ }); | ||
it('get server by roundrobin', function(done) { | ||
config.load(__dirname + path.sep + './config/idc_config.js', function(err, conf){ | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RoundRobinBalance(); | ||
var server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[1]); | ||
server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[0]); | ||
server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[1]); | ||
done(); | ||
}); | ||
it('get server by roundrobin', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RoundRobinBalance(); | ||
var server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[1]); | ||
server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[0]); | ||
server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService3.server[1]); | ||
}); | ||
it('direct use single server', function(done) { | ||
config.load(__dirname + path.sep + './config/singleserver_config.js', function(err, conf){ | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
var balance = new RoundRobinBalance(); | ||
var server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService.server[0]); | ||
(context.lastRoundRobinID === undefined).should.be.true; | ||
done(); | ||
}); | ||
it('direct use single server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/singleserver_config.js'); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
var balance = new RoundRobinBalance(); | ||
var server = balance.fetchServer(context); | ||
server.should.be.eql(conf.bookService.server[0]); | ||
(context.lastRoundRobinID === undefined).should.be.true; | ||
}); | ||
}); |
@@ -57,40 +57,25 @@ 'use strict'; | ||
it('load by file', function(done) { | ||
config.load(__dirname + path.sep + './config/single_config.js', function(err, conf){ | ||
(err === null).should.be.true; | ||
conf.should.have.properties('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
done(); | ||
}); | ||
it('load by file', function() { | ||
var conf = config.load(__dirname + path.sep + './config/single_config.js'); | ||
conf.should.have.properties('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
}); | ||
it('load by json', function(done) { | ||
config.load(__dirname + path.sep + './config/json_config.json', function(err, conf){ | ||
(err === null).should.be.true; | ||
conf.should.have.properties('bookService'); | ||
done(); | ||
}); | ||
it('load by json', function() { | ||
var conf = config.load(__dirname + path.sep + './config/json_config.json'); | ||
conf.should.have.properties('bookService'); | ||
}); | ||
it('load by wrong file path', function(done) { | ||
config.load(__dirname + path.sep + './config/single_config_w.js', function(err){ | ||
err.should.be.ok; | ||
done(); | ||
}); | ||
it('load by wrong file path', function() { | ||
(function(){config.load(__dirname + path.sep + './config/single_config_w.js');}).should.throwError(); | ||
}); | ||
it('load by wrong folder path', function(done) { | ||
config.load(__dirname + path.sep + './config/directory_w', function(err){ | ||
err.should.be.ok; | ||
done(); | ||
}); | ||
it('load by wrong folder path', function() { | ||
(function(){config.load(__dirname + path.sep + './config/directory_w');}).should.throwError(); | ||
}); | ||
it('load by directory', function(done) { | ||
config.load(__dirname + path.sep + './config/directory', function(err){ | ||
(err === null).should.be.true; | ||
var confs = config.getConfNames(); | ||
confs.should.containEql('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
done(); | ||
}); | ||
it('load by directory', function() { | ||
var conf = config.load(__dirname + path.sep + './config/directory'); | ||
var confs = config.getConfNames(); | ||
confs.should.containEql('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
}); | ||
}); |
@@ -55,3 +55,3 @@ /* | ||
it('should init successfully', function (done) { | ||
it('should init successfully', function () { | ||
RAL.init({ | ||
@@ -64,7 +64,4 @@ confDir : __dirname + path.sep + './ral/config', | ||
currentIDC : 'tc' | ||
}, function(err){ | ||
(err === undefined).should.be.true; | ||
isInited.emit('done'); | ||
done(); | ||
}); | ||
isInited.emit('done'); | ||
}); | ||
@@ -71,0 +68,0 @@ |
@@ -13,19 +13,14 @@ /* | ||
describe('ralmodule', function () { | ||
it('should load ext successfuly', function (done) { | ||
RalModule.load(__dirname + path.sep + '../lib/ext', function(err){ | ||
(err === null).should.be.ok; | ||
it('should load ext successfuly', function () { | ||
RalModule.load(__dirname + path.sep + '../lib/ext'); | ||
RalModule.modules.balance.random.should.be.ok; | ||
RalModule.modules.balance.roundrobin.should.be.ok; | ||
RalModule.modules.balance.random.should.be.ok; | ||
RalModule.modules.balance.roundrobin.should.be.ok; | ||
RalModule.modules.converter.form.should.be.ok; | ||
RalModule.modules.converter.json.should.be.ok; | ||
RalModule.modules.converter.string.should.be.ok; | ||
RalModule.modules.converter.urlencode.should.be.ok; | ||
RalModule.modules.converter.form.should.be.ok; | ||
RalModule.modules.converter.json.should.be.ok; | ||
RalModule.modules.converter.string.should.be.ok; | ||
RalModule.modules.converter.urlencode.should.be.ok; | ||
RalModule.modules.protocol.http.should.be.ok; | ||
done(); | ||
}); | ||
RalModule.modules.protocol.http.should.be.ok; | ||
}); | ||
}); |
222094
6048
7