Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling production/dev/testing configs in sails.js

Tags:

sails.js

Does anyone know the best way to handle prod/dev/test config switching in sails? I really like how actionhero.js automatically loads config/environment/{env}.js based on the contents of NODE_ENV but I don't see a built-in way to do something similar in sails. I noticed that sails will load any file in config/ during server bootstrap so my hacky solution for now is to setup the following:

config/
|-- local.js
|-- environment/
|---- production.js
|---- staging.js
|---- development.js
|---- testing.js

Then in each {env}.js file, I just extend config like this:

if (process.env.NODE_ENV === 'production') {
  // Enter any environment specific config changes
  config.db = {
    db_host: foo,
    db_port: bar
  }
  config.otherThing = {
    somevar: 'someval'
  }
like image 949
Jason Sims Avatar asked Feb 14 '14 22:02

Jason Sims


3 Answers

Since Sails 0.10-rc6 this got improved and you can now add an env subfolder in config to change settings for different environments.

So you could simply add a file /config/env/development.js or /config/env/production.js that can override all the necessary settings

See https://github.com/balderdashy/sails/pull/1638 for more details.

Example to change the port and database adapter, e.g. for production environment in your production.js:

module.exports = {
  port: 80
};

module.exports.models = {

  // Your app's default connection.
  // i.e. the name of one of your app's connections (see `config/connections.js`)
  //
  // (defaults to localDiskDb)
  connection: 'someMongodbServer'
};
like image 163
Markus Avatar answered Oct 29 '22 15:10

Markus


Simply go to config/env/production.js & set port and environment manually

And use NODE_ENV

For Windows use set NODE_ENV=production

For MAC/Linux use export NODE_ENV=production

OR

sails lift --prod

enter image description here

like image 12
Nishchit Avatar answered Oct 29 '22 14:10

Nishchit


I did this by making config/local.js require additional config files depending on process.env.NODE_ENV.

In config/local.js

var fs = require('fs'),
   lodash = require('lodash');

// config.local.js
module.exports = (function () {
  var defaults = {
    env: process.env.NODE_ENV || 'development',
    port: process.env.PORT || 1337,
    config: {
      paths: {
        environments: __dirname + '/environments'
      }
    }
  };

  var envConfigPath = defaults.config.paths.environments + '/' + defaults.env + '.js';
  var environment = {};

  if (fs.existsSync(envConfigPath)) {
    var environment = require(envConfigPath);
    logger.info('Loaded environment config for ' + defaults.env + '.');
  } else {
    logger.warn('Environment config for ' + defaults.env +' not found.');
  }
  return _.merge(defaults, environment);
}());

In .gitignore:

# config.local.js
config/environments

This gives you:

  • Overrides for any config based on your env by defining them in config/environments/{env}.js
  • sails.config{env} holding all your environment specific configuration, just as your solution does
  • Nice declarative {env} files
  • Warnings about missing {env} files
like image 6
marionebl Avatar answered Oct 29 '22 14:10

marionebl