From 5bfc3608560551fa2f127163e08067f18fe76b16 Mon Sep 17 00:00:00 2001 From: javi Date: Thu, 6 Mar 2014 15:19:12 +0100 Subject: [PATCH] added serverMetadata option for layer group, close #182 CDB-1940 --- NEWS.md | 1 + config/environments/production.js.example | 6 +++ config/environments/staging.js.example | 6 +++ lib/cartodb/server_options.js | 8 ++++ test/acceptance/multilayer.js | 34 +++++++++++++++++ test/acceptance/templates.js | 46 +++++++++++++++++++++++ 6 files changed, 101 insertions(+) diff --git a/NEWS.md b/NEWS.md index 56bd1665..96359cff 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,7 @@ Enhancements: - Add "cacheDns" statsd setting in the example configs - Do not send duplicated stats on template instanciation - Do not die on dns resolution errors (#178, #180) + - Allow to set server related configuration in serverMetadata (#182) Bug fixes: diff --git a/config/environments/production.js.example b/config/environments/production.js.example index ed12e57a..e828f2c8 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -116,6 +116,12 @@ var config = { // X-Tiler-Profile header containing elapsed timing for various // steps taken for producing the response. ,useProfiler:false + ,serverMetadata: { + cdn_url: { + http: 'api.cartocdn.com', + https: 'cartocdn.global.ssl.fastly.net' + } + } }; module.exports = config; diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index e098ba6d..dabaf9bf 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -116,6 +116,12 @@ var config = { // X-Tiler-Profile header containing elapsed timing for various // steps taken for producing the response. ,useProfiler:true + ,serverMetadata: { + cdn_url: { + http: 'api.cartocdn.com', + https: 'cartocdn.global.ssl.fastly.net' + } + } }; module.exports = config; diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index 80c32b74..ddcd7223 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -375,6 +375,14 @@ module.exports = function(){ } } + // include in layergroup response the variables in serverMedata + // those variables are useful to send to the client information + // about how to reach this server or information about it + var serverMetadata = global.environment.serverMetadata; + if (serverMetadata) { + _.extend(response, serverMetadata); + } + // Don't wait for the mapview count increment to // take place before proceeding. Error will be logged // asyncronously diff --git a/test/acceptance/multilayer.js b/test/acceptance/multilayer.js index c248e8d6..c8c449fd 100644 --- a/test/acceptance/multilayer.js +++ b/test/acceptance/multilayer.js @@ -192,6 +192,40 @@ suite('multilayer', function() { ); }); + + test("should include serverMedata in the response", function(done) { + global.environment.serverMetadata = { cdn_url : { http:'test', https: 'tests' } } + var layergroup = { + version: '1.0.0', + layers: [ + { options: { + sql: 'select cartodb_id, ST_Translate(the_geom_webmercator, 5e6, 0) as the_geom_webmercator from test_table limit 2', + cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }', + cartocss_version: '2.0.1' + } } + ] + }; + + var expected_token; + Step( + function do_create_get() + { + var next = this; + assert.response(server, { + url: '/tiles/layergroup?config=' + encodeURIComponent(JSON.stringify(layergroup)), + method: 'GET', + headers: {host: 'localhost'} + }, {}, function(res, err) { next(err, res); }); + }, + function do_check_create(err, res) { + var parsed = JSON.parse(res.body); + assert.ok(_.isEqual(parsed.cdn_url, global.environment.serverMetadata.cdn_url)); + done(); + } + ) + }); + + // See https://github.com/CartoDB/Windshaft-cartodb/issues/176 // NOTE: another test like this is in templates.js test("get creation requests no cache", function(done) { diff --git a/test/acceptance/templates.js b/test/acceptance/templates.js index a75bf82f..d78ef637 100644 --- a/test/acceptance/templates.js +++ b/test/acceptance/templates.js @@ -308,6 +308,52 @@ suite('template_api', function() { }); }); + test("instance endpoint should return server metadata", function(done){ + global.environment.serverMetadata = { cdn_url : { http:'test', https: 'tests' } } + var tmpl = _.clone(template_acceptance1) + tmpl.name = "rambotemplate2" + + Step(function postTemplate1(err, res) { + var next = this; + var post_request = { + url: '/tiles/template?api_key=1234', + method: 'POST', + headers: {host: 'localhost', 'Content-Type': 'application/json' }, + data: JSON.stringify(tmpl) + }; + assert.response(server, post_request, {}, function(res) { + next(null, res); + }); + }, + function testCORS() { + var next = this; + assert.response(server, { + url: '/tiles/template/' + tmpl.name, + method: 'POST', + headers: {host: 'localhost', 'Content-Type': 'application/json' }, + },{ + status: 200 + }, function(res) { + var parsed = JSON.parse(res.body); + assert.ok(_.isEqual(parsed.cdn_url, global.environment.serverMetadata.cdn_url)); + next(null); + }); + }, + function deleteTemplate(err) { + if ( err ) throw err; + var del_request = { + url: '/tiles/template/' + tmpl.name + '?api_key=1234', + method: 'DELETE', + headers: {host: 'localhost', 'Content-Type': 'application/json' } + } + var next = this; + assert.response(server, del_request, {}, + function(res) { done(); }); + } + ); + }); + + test("can list templates", function(done) {