diff --git a/lib/application.js b/lib/application.js index 347004db..51896c9a 100644 --- a/lib/application.js +++ b/lib/application.js @@ -231,6 +231,25 @@ app.boot = function(options) { assertIsValidConfig('model', modelConfig); assertIsValidConfig('data source', dataSourceConfig); + appConfig.host = + process.env.npm_config_host || + process.env.OPENSHIFT_SLS_IP || + process.env.OPENSHIFT_NODEJS_IP || + process.env.HOST || + appConfig.host || + process.env.npm_package_config_host || + app.get('host'); + + appConfig.port = + process.env.npm_config_port || + process.env.OPENSHIFT_SLS_PORT || + process.env.OPENSHIFT_NODEJS_PORT || + process.env.PORT || + appConfig.port || + process.env.npm_package_config_port || + app.get('port') || + 3000; + if(appConfig.host !== undefined) { assert(typeof appConfig.host === 'string', 'app.host must be a string'); app.set('host', appConfig.host); diff --git a/test/app.test.js b/test/app.test.js index bf47ff30..3787503a 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -77,6 +77,68 @@ describe('app', function() { assert.equal(this.app.get('host'), '127.0.0.1'); }); + describe('PaaS and npm env variables', function() { + beforeEach(function() { + this.boot = function () { + var app = loopback(); + app.boot({ + app: { + port: undefined, + host: undefined + } + }); + return app; + } + }); + + it('should be honored', function() { + var assertHonored = function (portKey, hostKey) { + process.env[hostKey] = randomPort(); + process.env[portKey] = randomHost(); + var app = this.boot(); + assert.equal(app.get('port'), process.env[portKey]); + assert.equal(app.get('host'), process.env[hostKey]); + delete process.env[portKey]; + delete process.env[hostKey]; + }.bind(this); + + assertHonored('OPENSHIFT_SLS_PORT', 'OPENSHIFT_NODEJS_IP'); + assertHonored('npm_config_port', 'npm_config_host'); + assertHonored('npm_package_config_port', 'npm_package_config_host'); + assertHonored('OPENSHIFT_SLS_PORT', 'OPENSHIFT_SLS_IP'); + assertHonored('PORT', 'HOST'); + }); + + it('should be honored in order', function() { + process.env.npm_config_host = randomHost(); + process.env.OPENSHIFT_SLS_IP = randomHost(); + process.env.OPENSHIFT_NODEJS_IP = randomHost(); + process.env.HOST = randomHost(); + process.env.npm_package_config_host = randomHost(); + + var app = this.boot(); + assert.equal(app.get('host'), process.env.npm_config_host); + + process.env.npm_config_port = randomPort(); + process.env.OPENSHIFT_SLS_PORT = randomPort(); + process.env.OPENSHIFT_NODEJS_PORT = randomPort(); + process.env.PORT = randomPort(); + process.env.npm_package_config_port = randomPort(); + + var app = this.boot(); + assert.equal(app.get('host'), process.env.npm_config_host); + assert.equal(app.get('port'), process.env.npm_config_port); + }); + + function randomHost() { + return Math.random().toString().split('.')[1]; + } + + function randomPort() { + return Math.floor(Math.random() * 10000); + } + }); + it('Instantiate models', function () { assert(app.models); assert(app.models.FooBarBatBaz);