From ca4f3d20255336358e2d709b15bfc201be745119 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 30 Jan 2014 16:09:13 +0100 Subject: [PATCH] Re-introduce sqlapi.host directive, allowing DNS lookups drop For backward compatibility, sqlapi.host is only used if domain is also defined and has a different value (empty string allowed). Closes #117 --- NEWS.md | 7 +++++++ config/environments/development.js.example | 9 ++++++++- config/environments/production.js.example | 9 ++++++++- config/environments/staging.js.example | 9 ++++++++- config/environments/test.js.example | 11 +++++++++-- lib/cartodb/server_options.js | 23 ++++++++++++++++------ package.json | 2 +- 7 files changed, 58 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index 77213d63..6f7460f3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +1.6.4 -- 2014-MM-DD +------------------- + +Enhancements: + +* Allow specifying fixed sqlapi host address (#117) + 1.6.3 -- 2014-01-30 ------------------- diff --git a/config/environments/development.js.example b/config/environments/development.js.example index 97941dd7..c47ac82b 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -64,8 +64,15 @@ var config = { } ,sqlapi: { protocol: 'http', - domain: 'localhost.lan', + // If "host" is given, it will be used + // to connect to the SQL-API without a + // DNS lookup + host: '127.0.0.1', port: 8080, + // The "domain" part will be appended to + // the cartodb username and passed to + // SQL-API requests in the Host HTTP header + domain: '', version: 'v1' } ,varnish: { diff --git a/config/environments/production.js.example b/config/environments/production.js.example index e64ad4ea..a636d697 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -58,8 +58,15 @@ var config = { } ,sqlapi: { protocol: 'https', - domain: 'cartodb.com', + // If "host" is given, it will be used + // to connect to the SQL-API without a + // DNS lookup + //host: '127.0.0.1', port: 8080, + // The "domain" part will be appended to + // the cartodb username and passed to + // SQL-API requests in the Host HTTP header + domain: 'cartodb.com', version: 'v2' } ,varnish: { diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 8695ccfc..97ede9a5 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -58,8 +58,15 @@ var config = { } ,sqlapi: { protocol: 'https', - domain: 'cartodb.com', + // If "host" is given, it will be used + // to connect to the SQL-API without a + // DNS lookup + //host: '127.0.0.1', port: 8080, + // The "domain" part will be appended to + // the cartodb username and passed to + // SQL-API requests in the Host HTTP header + domain: 'cartodb.com', version: 'v2' } ,varnish: { diff --git a/config/environments/test.js.example b/config/environments/test.js.example index f9bd3929..c523ba7d 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -58,10 +58,17 @@ var config = { } ,sqlapi: { protocol: 'http', - domain: '', + // If "host" is given, it will be used + // to connect to the SQL-API without a + // DNS lookup + host: '127.0.0.1', + port: 1080, + // The "domain" part will be appended to + // the cartodb username and passed to + // SQL-API requests in the Host HTTP header + domain: 'donot_look_this_up', // This port will be used by "make check" for testing purposes // It must be available - port: 1080, version: 'v1' } ,varnish: { diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index f0123543..02065813 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -10,7 +10,11 @@ var _ = require('underscore') // This is for backward compatibility with 1.3.3 if ( _.isUndefined(global.environment.sqlapi.domain) ) { - global.environment.sqlapi.domain = global.environment.sqlapi.host; + // Only use "host" as "domain" if it contains alphanumeric characters + var host = global.environment.sqlapi.host; + if ( host && host.match(/[a-zA-Z]/) ) { + global.environment.sqlapi.domain = host; + } } module.exports = function(){ @@ -68,9 +72,13 @@ module.exports = function(){ var api = global.environment.sqlapi; // build up api string - var sqlapi = api.protocol + '://' + username; - if ( api.domain ) sqlapi += '.' + api.domain; - sqlapi += ':' + api.port + '/api/' + api.version + '/sql' + var sqlapihostname = username; + if ( api.domain ) sqlapihostname += '.' + api.domain; + + var sqlapi = api.protocol + '://'; + if ( api.host && api.host != api.domain ) sqlapi += api.host; + else sqlapi += sqlapihostname; + sqlapi += ':' + api.port + '/api/' + api.version + '/sql'; var qs = { q: sql } @@ -85,8 +93,11 @@ module.exports = function(){ // TODO: use "host" header to allow IP based specification // of sqlapi address (and avoid a DNS lookup) // - request.post({url:sqlapi, body:qs, json:true}, - function(err, res, body){ + request.post({ + url:sqlapi, body:qs, json:true, + headers:{hostname:sqlapihostname} + }, function(err, res, body) + { if (err){ console.log('ERROR connecting to SQL API on ' + sqlapi + ': ' + err); callback(err); diff --git a/package.json b/package.json index cd2cef2c..ee15a874 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "windshaft-cartodb", - "version": "1.6.3", + "version": "1.6.4", "description": "A map tile server for CartoDB", "keywords": [ "cartodb"