Skip to content

Commit

Permalink
Merge pull request #154 from forio/api-server-config
Browse files Browse the repository at this point in the history
EPICENTER-1725: Load the api configuration from the server
  • Loading branch information
rvillarreal committed Apr 11, 2016
2 parents fef8f7a + e4ec777 commit 1e59ecb
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var F = {

};

F.load = require('./env-load');
F.load();

F.util.query = require('./util/query-util');
F.util.makeSequence = require('./util/make-sequence');
F.util.run = require('./util/run-util');
Expand Down
26 changes: 26 additions & 0 deletions src/env-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

var urlConfigService = require('./service/url-config-service');

var envLoad = function (callback) {
var envPromise;
var host;
var urlService = urlConfigService();
var envPath = '/epicenter/v1/config';
if (urlService.isLocalhost()) {
envPromise = $.Deferred();
envPromise.resolve();
host = 'https://forio.com';
} else {
host = '';
}
var infoUrl = host + envPath;
envPromise = $.ajax({ url: infoUrl, async: false });
envPromise.done(function (res) {
var api = res.api;
$.extend(urlConfigService, api);
});
envPromise.done(callback).fail(callback);
};

module.exports = envLoad;
12 changes: 10 additions & 2 deletions src/service/url-config-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var epiVersion = require('../api-version.json');

module.exports = function (config) {
var UrlConfigService = function (config) {
//TODO: urlutils to get host, since no window on node

var API_PROTOCOL = 'https';
Expand Down Expand Up @@ -70,6 +70,14 @@ module.exports = function (config) {
}
};

$.extend(publicExports, config);
// This data is set by an external script (start-load.js)
var envConf = {
protocol: UrlConfigService.protocol,
host: UrlConfigService.host
};

$.extend(publicExports, envConf, config);
return publicExports;
};

module.exports = UrlConfigService;
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<script src="spec/test-channel-service.js"></script>
<script src="spec/test-channel-manager.js"></script>
<script src="spec/test-epicenter-channel-manager.js"></script>
<script src="spec/test-env-load.js"></script>

<script>
// If tests run in a real browser
Expand Down
47 changes: 47 additions & 0 deletions tests/spec/test-env-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(function () {
'use strict';
describe('Env Load', function () {
var server;
beforeEach(function () {
server = sinon.fakeServer.create();

var env = {
api: {
host: 'api.forio.com',
protocol: 'https'
}
};
// API Config
server.respondWith('GET', /forio\.com\/epicenter\/v1\/config/, function (xhr, id) {
xhr.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(env));
});

server.autoRespond = true;
});

afterEach(function () {
server.restore();
});

describe('load', function () {
it('it should request the server env url', function () {
F.load();

var req = server.requests.pop();
req.method.toUpperCase().should.equal('GET');
req.url.should.equal('https://forio.com/epicenter/v1/config');
});

it('it should set protocol and host to the UrlConfingService', function () {
delete F.service.URL.protocol;
delete F.service.URL.host;
F.load(function () {
F.service.URL.protocol.should.equal('https');
F.service.URL.host.should.equal('api.forio.com');
delete F.service.URL.protocol;
delete F.service.URL.host;
});
});
});
});
})();
11 changes: 11 additions & 0 deletions tests/spec/test-url-config-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
var url = new URLService({ accountPath: 'forioAccount', projectPath: 'forioProj', versionPath: '' });
url.getAPIPath('data').should.equal('https://api.forio.com/data/forioAccount/forioProj/');
});

it('should allow over-riding host and protocol globally', function () {
URLService.protocol = 'htttps';
URLService.host = 'funky.forio.com';
var url = new URLService({ accountPath: 'forioAccount', projectPath: 'forioProj', versionPath: '' });
url.getAPIPath('data').should.equal('htttps://funky.forio.com/data/forioAccount/forioProj/');

// Delete global settings to avoid affecting other tests
delete F.service.URL.protocol;
delete F.service.URL.host;
});
});
});
})();

0 comments on commit 1e59ecb

Please sign in to comment.