SLA-725 support PORT and HOST environment for PaaS support

This commit is contained in:
Ritchie 2013-12-05 17:42:06 -08:00
parent e554a07fe0
commit 68ef03e944
2 changed files with 81 additions and 0 deletions

View File

@ -231,6 +231,25 @@ app.boot = function(options) {
assertIsValidConfig('model', modelConfig); assertIsValidConfig('model', modelConfig);
assertIsValidConfig('data source', dataSourceConfig); 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) { if(appConfig.host !== undefined) {
assert(typeof appConfig.host === 'string', 'app.host must be a string'); assert(typeof appConfig.host === 'string', 'app.host must be a string');
app.set('host', appConfig.host); app.set('host', appConfig.host);

View File

@ -77,6 +77,68 @@ describe('app', function() {
assert.equal(this.app.get('host'), '127.0.0.1'); 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 () { it('Instantiate models', function () {
assert(app.models); assert(app.models);
assert(app.models.FooBarBatBaz); assert(app.models.FooBarBatBaz);