All files / src/utils Configuration.js

78.57% Statements 11/14
57.14% Branches 4/7
100% Functions 1/1
78.57% Lines 11/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118    1x 1x   1x                                                                                                                                                                     34x     34x 34x       34x 34x                 34x             34x     1x  
'use strict';
 
const convict = require('convict');
const argv = require('yargs').argv;
 
const SETTINGS_SCHEMA = {
  port: {
    doc: 'Listening port',
    format: 'port',
    default: 8080,
    env: 'PORT',
    arg: 'port',
  },
  env: {
    doc: 'Application environment',
    format: String,
    default: 'dev',
    env: 'NODE_ENV',
    arg: 'env',
  },
  api_base_path: {
    doc: 'Base path of the mock files',
    format: String,
    default: './mocks',
    arg: 'api-base-path',
    env: 'API_BASE_PATH',
  },
  http_codes: {
    mock_file_not_found: {
      doc: 'HTTP code returned in case of a mock file that could not be found',
      format: 'int',
      default: 404,
    },
    mock_file_invalid: {
      doc: 'HTTP code return in case of an invalid mock file (that could not be parsed or interpreted)',
      format: 'int',
      default: 500,
    }
  },
  authorization_token: {
    doc: 'Authorization token type (base64)',
    format: String,
    default: '',
    arg: 'authorization-token',
    env: 'AUTHORIZATION_TOKEN',
  },
  dynamic_markers: {
    doc: 'Additionnals API path markers',
    format: Array,
    default: [],
    arg: 'api-dynamic-markers',
    env: 'API_DYNAMIC_MARKERS',
  },
  logs: {
    doc: 'Logs configuration as an object or an array of node-bunyan streams',
    format: Array,
    default: [{
      type:  'rotating-file',
      path: './logs/api-mockiji.log',
      level: 'warn',
      period:  '1d',
      count:  3,
    }]
  },
  middlewares: {
    doc: 'Additional Express middlewares',
    format: Array,
    default: []
  },
  silent: {
    doc: 'If set to true the default stdout log stream will not be created',
    format: Boolean,
    default: false,
    arg: 'silent',
  }
}
 
/**
 * This function creates and returns a new node-convict configuration.
 * It tries to load settings using the following sources :
 * - default values as defined in the SETTINGS_SCHEMA constant
 * - CLI arguments as defined in the SETTINGS_SCHEMA constant
 * - Environment variables as defined in the SETTINGS_SCHEMA constant
 * - inline configuration passed through the 'configuration' option
 * - JSON file whose path is provided in the 'configFile' option or in the '--config-file' CLI argument
 */
function initConfiguration({configuration, configFile} = {}) {
  // Initialize default configuration
  const config = convict(SETTINGS_SCHEMA);
 
  // If there is a provided configuration
  Eif (configuration) {
    config.load(configuration);
  }
 
  // Load an optional config file (e.g.: node app.js --config-file config.json)
  configFile = configFile || argv.configFile;
  Iif (configFile) {
    try {
      config.loadFile(configFile);
    } catch (e) {
      throw e;
    }
  }
 
  // Validate configuration
  config.validate({allowed: 'strict'});
 
  // Return convict object so it can directly be used
  // to retrieve settings.
  //
  //   Configuration.get('env');
  //
  return config;
}
 
module.exports = initConfiguration;