<commit_before>var
  Broker = require('../service/Broker'),
  HttpProxy = require('../service/HttpProxy'),
  Context = require('../core/Context'),
  fs = require('fs'),
  path = require('path'),
  _ = require('lodash');

/**
 * KuzzleProxy constructor
 *
 * @param {ProxyBackendHandler} BackendHandler
 * @param {String} applicationName
 * @constructor
 */
function KuzzleProxy (BackendHandler, applicationName) {
  /** @type {Object} */
  this.config = this.getRCConfig(applicationName);

  /** @type {Context} */
  this.context = new Context(BackendHandler, this.config.backendMode);

  /** @type {Broker} */
  this.broker = new Broker();

  /** @type {HttpProxy} */
  this.httpProxy = new HttpProxy();

  /** @type {Boolean} */
  this.isDummy = false;
}

/**
 * Returns RC configuration
 *
 * @returns {Object}
 */
KuzzleProxy.prototype.getRCConfig = function (applicationName) {
  if (applicationName === undefined) {
    applicationName = 'proxy';
  }

  return require('rc')(applicationName, {
    protocolPlugins: {},
    backendOptions: {},
    backendMode: 'standard',
    httpPort: 7511,
    backendTimeout: 10000
  });
};

/**
 * @param {String} rootFolder
 * Reads configuration files and initializes plugins
 */
KuzzleProxy.prototype.initPlugins = function (rootFolder) {
  var pluginsConfiguration = this.readPluginsConfiguration(rootFolder);

  _.forEach(pluginsConfiguration, (pluginConfiguration, pluginName) => {
    var
      plugin;

    if (pluginConfiguration.activated) {
      console.log(`Initializing plugin protocol ${pluginName}`);
      try {
        plugin = new (this.requirePluginPackage(rootFolder, pluginName))();
        plugin.init(pluginConfiguration, this.context, this.isDummy);

        this.context.pluginStore.add(plugin);
      } catch (error) {
        console.error(`Initialization of plugin ${pluginName} has failed; Reason :`, error);
      }
    }
  });

  if (this.context.pluginStore.count() === 0) {
    throw new Error('No plugin has been initialized properly. Shutting down.');
  }
};

/**
 * Intializes the broker
 */
KuzzleProxy.prototype.initBroker = function () {

  this.context.setBroker(this.broker);
  this.broker.init(this.context, this.config.backendOptions, this.config.backendTimeout);
};

/**
 * Initializes the httpProxy
 */
KuzzleProxy.prototype.initHttpProxy = function () {
  this.httpProxy.init(this.config.backendMode, this.context, this.config.httpPort);
};

/**
 * Updates plugins configuration in Kuzzle database
 *
 * @param {String} rootFolder
 * @returns {Object}
 */
KuzzleProxy.prototype.readPluginsConfiguration = function (rootFolder) {
  var
    pluginsConfiguration = {},
    currentConfig,
    plugins = this.config.protocolPlugins,
    activatedPlugins;

  try {
    currentConfig = this.loadCurrentConfig(rootFolder);
  }
  catch (error) {
    throw error;
  }

  if (Object.keys(plugins).length === 0) {
    throw new Error('No plugin configuration provided. Shutting down.');
  }

  Object.keys(plugins).forEach((pluginName) => {
    pluginsConfiguration[pluginName] = this.readOnePluginConfiguration(currentConfig, pluginName);
  });

  activatedPlugins = Object
    .keys(pluginsConfiguration)<commit_msg>Make rootFolder configurable everywhere in order to implement LB properly on top of proxy<commit_after>var
  Broker = require('../service/Broker'),
  HttpProxy = require('../service/HttpProxy'),
  Context = require('../core/Context'),
  fs = require('fs'),
  path = require('path'),
  _ = require('lodash');

/**
 * KuzzleProxy constructor
 *
 * @param {ProxyBackendHandler} BackendHandler
 * @param {String} applicationName
 * @constructor
 */
function KuzzleProxy (BackendHandler, applicationName) {
  /** @type {Object} */
  this.config = this.getRCConfig(applicationName);

  /** @type {Context} */
  this.context = new Context(BackendHandler, this.config.backendMode);

  /** @type {Broker} */
  this.broker = new Broker();

  /** @type {HttpProxy} */
  this.httpProxy = new HttpProxy();

  /** @type {Boolean} */
  this.isDummy = false;
}

/**
 * Returns RC configuration
 *
 * @returns {Object}
 */
KuzzleProxy.prototype.getRCConfig = function (applicationName) {
  if (applicationName === undefined) {
    applicationName = 'proxy';
  }

  return require('rc')(applicationName, {
    protocolPlugins: {},
    backendOptions: {},
    backendMode: 'standard',
    httpPort: 7511,
    backendTimeout: 10000
  });
};

/**
 * @param {String} rootFolder
 * Reads configuration files and initializes plugins
 */
KuzzleProxy.prototype.initPlugins = function (rootFolder) {
  var pluginsConfiguration = this.readPluginsConfiguration(rootFolder);

  _.forEach(pluginsConfiguration, (pluginConfiguration, pluginName) => {
    var
      plugin;

    if (pluginConfiguration.activated) {
      console.log(`Initializing plugin protocol ${pluginName}`);
      try {
        plugin = new (this.requirePluginPackage(rootFolder, pluginName))();
        plugin.init(pluginConfiguration, this.context, this.isDummy);

        this.context.pluginStore.add(plugin);
      } catch (error) {
        console.error(`Initialization of plugin ${pluginName} has failed; Reason :`, error);
      }
    }
  });

  if (this.context.pluginStore.count() === 0) {
    throw new Error('No plugin has been initialized properly. Shutting down.');
  }
};

/**
 * Intializes the broker
 */
KuzzleProxy.prototype.initBroker = function () {

  this.context.setBroker(this.broker);
  this.broker.init(this.context, this.config.backendOptions, this.config.backendTimeout);
};

/**
 * Initializes the httpProxy
 */
KuzzleProxy.prototype.initHttpProxy = function () {
  this.httpProxy.init(this.config.backendMode, this.context, this.config.httpPort);
};

/**
 * Updates plugins configuration in Kuzzle database
 *
 * @param {String} rootFolder
 * @returns {Object}
 */
KuzzleProxy.prototype.readPluginsConfiguration = function (rootFolder) {
  var
    pluginsConfiguration = {},
    currentConfig,
    plugins = this.config.protocolPlugins,
    activatedPlugins;

  try {
    currentConfig = this.loadCurrentConfig(rootFolder);
  }
  catch (error) {
    throw error;
  }

  if (Object.keys(plugins).length === 0) {
    throw new Error('No plugin configuration provided. Shutting down.');
  }

  Object.keys(plugins).forEach((pluginName) => {
    pluginsConfiguration[pluginName] = this.readOnePluginConfiguration(rootFolder, currentConfig, pluginName);
  });

  activatedPlugins = Object
    .keys(pluginsConfiguration)
{'fraction_non_alphanumeric': 0.1390432098765432, 'fraction_numerical': 0.0033950617283950617, 'mean_word_length': 3.9210326499620347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7451322', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1873, 'n_words': 397}