Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // Hierarchical node.js configuration with command-line arguments, environment
  4. // variables, and files.
  5. const nconf = module.exports = require('nconf');
  6. const path = require('path');
  7.  
  8. nconf
  9. // 1. Command-line arguments
  10. .argv()
  11. // 2. Environment variables
  12. .env([
  13. 'DATA_BACKEND',
  14. 'GCLOUD_PROJECT',
  15. 'MONGO_URL',
  16. 'MONGO_COLLECTION',
  17. 'MYSQL_USER',
  18. 'MYSQL_PASSWORD',
  19. 'PORT'
  20. ])
  21. // 3. Config file
  22. .file({ file: path.join(__dirname, 'config.json') })
  23. // 4. Defaults
  24. .defaults({
  25. // dataBackend can be 'datastore', 'cloudsql', or 'mongodb'. Be sure to
  26. // configure the appropriate settings for each storage engine below.
  27. // If you are unsure, use datastore as it requires no additional
  28. // configuration.
  29. DATA_BACKEND: 'datastore',
  30.  
  31. // This is the id of your project in the Google Cloud Developers Console.
  32. GCLOUD_PROJECT: '',
  33.  
  34. // MongoDB connection string
  35. // https://docs.mongodb.org/manual/reference/connection-string/
  36. MONGO_URL: 'mongodb://localhost:27017',
  37. MONGO_COLLECTION: 'books',
  38.  
  39. MYSQL_USER: '',
  40. MYSQL_PASSWORD: '',
  41.  
  42. // Port the HTTP server
  43. PORT: 1010
  44. });
  45.  
  46. // Check for required settings
  47. checkConfig('GCLOUD_PROJECT');
  48.  
  49. if (nconf.get('DATA_BACKEND') === 'cloudsql') {
  50. checkConfig('MYSQL_USER');
  51. checkConfig('MYSQL_PASSWORD');
  52. if (nconf.get('NODE_ENV') === 'production') {
  53. checkConfig('INSTANCE_CONNECTION_NAME');
  54. }
  55. } else if (nconf.get('DATA_BACKEND') === 'mongodb') {
  56. checkConfig('MONGO_URL');
  57. checkConfig('MONGO_COLLECTION');
  58. }
  59.  
  60. function checkConfig (setting) {
  61. if (!nconf.get(setting)) {
  62. throw new Error(`You must set ${setting} as an environment variable or in config.json!`);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement