config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var path = require('path');
  18. var fs = require('fs-extra');
  19. var mkdirp = require('mkdirp');
  20. var memdbLogger = require('memdb-logger');
  21. var logger = memdbLogger.getLogger('memdb', __filename);
  22. var utils = require('./utils');
  23. var _config = null;
  24. exports.init = function(confPath, shardId){
  25. var searchPaths = [];
  26. var homePath = process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH;
  27. var localDataPath = path.join(homePath, '.memdb');
  28. mkdirp(localDataPath);
  29. searchPaths = confPath ? [confPath] : [path.join(homePath, '.memdb/memdb.conf.js'), '/etc/memdb.conf.js'];
  30. var conf = null;
  31. for(var i=0; i<searchPaths.length; i++){
  32. if(fs.existsSync(searchPaths[i])){
  33. confPath = path.resolve(searchPaths[i]);
  34. conf = require(confPath);
  35. exports.path = confPath;
  36. break;
  37. }
  38. }
  39. if(!conf){
  40. if(confPath){
  41. throw new Error('config file not found - ' + searchPaths);
  42. }
  43. // copy and load default config
  44. var confTemplatePath = path.join(__dirname, '../memdb.conf.js');
  45. var defaultConfPath = path.join(localDataPath, 'memdb.conf.js');
  46. fs.copySync(confTemplatePath, defaultConfPath);
  47. conf = require(defaultConfPath);
  48. }
  49. // Configure promise
  50. if(conf.promise && conf.promise.longStackTraces){
  51. P.longStackTraces();
  52. }
  53. // Configure log
  54. var logConf = conf.log || {};
  55. var logPath = logConf.path || path.join(localDataPath, 'log');
  56. mkdirp(logPath);
  57. console.log('log path: %s', logPath);
  58. memdbLogger.configure(path.join(__dirname, 'log4js.json'), {shardId : shardId || '$', base : logPath});
  59. var level = logConf.level || 'INFO';
  60. memdbLogger.setGlobalLogLevel(memdbLogger.levels[level]);
  61. // heapdump
  62. if(conf.heapdump){
  63. require('heapdump');
  64. }
  65. _config = conf;
  66. };
  67. exports.getShardIds = function(){
  68. if(!_config){
  69. throw new Error('please config.init first');
  70. }
  71. return Object.keys(_config.shards);
  72. };
  73. exports.shardConfig = function(shardId){
  74. if(!_config){
  75. throw new Error('please config.init first');
  76. }
  77. var conf = utils.clone(_config);
  78. var shardConf = conf.shards && conf.shards[shardId];
  79. if(!shardConf){
  80. throw new Error('shard ' + shardId + ' not configured');
  81. }
  82. // Override shard specific config
  83. for(var key in shardConf){
  84. conf[key] = shardConf[key];
  85. }
  86. conf.shardId = shardId;
  87. return conf;
  88. };
  89. exports.clusterConfig = function(){
  90. return _config;
  91. };