Component

Node.js standard app configuration

Mar 16th, 2014
262
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright (c) TySoft Limited 2014 - ALL RIGHTS RESERVED.
  3.  */
  4.  
  5. 'use strict';
  6.  
  7. var _ = require('underscore');
  8.  
  9. /**
  10.  * Config keys.
  11.  */
  12. var keys = exports.keys = {
  13.     port: 'port',
  14.     server_url: 'server_url',
  15.     session_key: 'session_key',
  16.     session_secret: 'session_secret',
  17.     database_dsn: 'database_dsn',
  18.     redis_dsn: 'redis_dsn',
  19.     encryption_two_way_key: 'encryption_two_way_key',
  20.     github_client_id: 'github_client_id',
  21.     github_client_secret: 'github_client_secret',
  22.     google_client_id: 'google_client_id',
  23.     google_client_secret: 'google_client_secret'
  24. };
  25.  
  26. /**
  27.  * Config values that must be configured when running in production.
  28.  */
  29. var required = [
  30.     keys.session_secret,
  31.     keys.encryption_two_way_key,
  32.     keys.server_url,
  33.     keys.database_dsn,
  34.     keys.redis_dsn,
  35.     keys.github_client_id,
  36.     keys.github_client_secret,
  37.     keys.google_client_id,
  38.     keys.google_client_secret];
  39.  
  40. /**
  41.  * Default configuration values.
  42.  */
  43. var defaults = {};
  44. (function () {
  45.     defaults[keys.port] = 4000;
  46.     defaults[keys.server_url] = 'http://localhost:' + defaults[keys.port];
  47.     defaults[keys.session_key] = 'poker';
  48.     defaults[keys.session_secret] = 'CHANGE_ME_FOOL';
  49.     defaults[keys.database_dsn] = 'mongodb://localhost/test';
  50.     defaults[keys.redis_dsn] = 'redis://127.0.0.1:6379/0';
  51.     defaults[keys.encryption_two_way_key] = 'CHANGE_ME_FOOL';
  52.     defaults[keys.github_client_id] = '8c33e3fe8f1633a9a3';
  53.     defaults[keys.github_client_secret] = '281bfe2ssdf1ba5f2b2e1a2afsa1b08d402e8';
  54.     defaults[keys.google_client_id] = '1226332829-h2m4i4zdsssd3uufvfbbk9kccql4r.apps.googleusercontent.com';
  55.     defaults[keys.google_client_secret] = 'PNlfDD@#22FOK-rwO4SjOH';
  56. }());
  57.  
  58. /**
  59.  * Gets the value for a config var.
  60.  * @param key The name of the variable to get the value of.
  61.  */
  62. var getConfig = exports.getConfig = function (key) {
  63.  
  64.     var env_var = process.env[key.toUpperCase()];
  65.     if (!_.isUndefined(env_var)) {
  66.         return env_var;
  67.     }
  68.  
  69.     var default_val = defaults[key];
  70.     if (!_.isUndefined(default_val)) {
  71.         return default_val;
  72.     }
  73.  
  74.     return null;
  75. };
  76.  
  77. /**
  78.  * Returns true if running in production.
  79.  */
  80. var inProduction = exports.inProduction = function () {
  81.     return process.env.NODE_ENV === 'production';
  82. };
  83.  
  84. /**
  85.  * Checks the environment config and warns where default values are in use. Will forcefully exit if running in
  86.  * production and encryption keys have not been changed from the default.
  87.  */
  88. exports.checkConfig = function () {
  89.  
  90.     _.each(defaults, function (value, key) {
  91.         if (getConfig(key) === value) {
  92.  
  93.             // Throw if required values haven't been configured in production
  94.             if (required.indexOf(key) !== -1 && inProduction()) {
  95.                 throw new Error(key + ' hasn\'t been configured but has been marked as required for production.');
  96.             }
  97.  
  98.             console.log(key + ' hasn\'t been configured, using default value: ' + value);
  99.         }
  100.     });
  101. };
RAW Paste Data