Advertisement
Guest User

Untitled

a guest
Apr 30th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nconf = module.exports = require('nconf');
  2. const path = require('path');
  3.  
  4. nconf
  5. // 1. Command-line arguments
  6.     .argv()
  7.     // 2. Environment variables
  8.     .env([
  9.         'DATA_BACKEND',
  10.         'GCLOUD_PROJECT',
  11.         'MYSQL_USER',
  12.         'MYSQL_PASSWORD',
  13.         'MYSQL_HOST',
  14.         'PORT',
  15.         'INSTANCE_CONNECTION_NAME',
  16.         'NODE_ENV'
  17.     ])
  18.     // 3. Config file
  19.     .file({ file: path.join(__dirname, 'config.json') })
  20.     // 4. Defaults
  21.     .defaults({
  22.         // dataBackend can be 'datastore', 'cloudsql', or 'mongodb'. Be sure to
  23.         // configure the appropriate settings for each storage engine below.
  24.         // If you are unsure, use datastore as it requires no additional
  25.         // configuration.
  26.         DATA_BACKEND: 'cloudsql',
  27.         // This is the id of your project in the Google Cloud Developers Console.
  28.         GCLOUD_PROJECT: 'bookingsystem2',
  29.         MYSQL_USER: 'my user',
  30.         MYSQL_PASSWORD: 'my password',
  31.         MYSQL_HOST: 'sql ip address',
  32.         PORT: 3306,
  33.         INSTANCE_CONNECTION_NAME: 'my ICN',
  34.         NODE_ENV: 'production'
  35.     });
  36.  
  37.  
  38. // Check for required settings
  39. checkConfig('GCLOUD_PROJECT');
  40.  
  41.  
  42. if (nconf.get('DATA_BACKEND') === 'cloudsql') {
  43.     checkConfig('MYSQL_USER');
  44.     checkConfig('MYSQL_PASSWORD');
  45.     if (nconf.get('NODE_ENV') === 'production') {
  46.         checkConfig('INSTANCE_CONNECTION_NAME');
  47.     }
  48. }
  49.  
  50. function checkConfig (setting) {
  51.     if (!nconf.get(setting)) {
  52.         throw new Error('You must set ' + setting + ' as an environment variable or in config.json!');
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement