From 8d1b394df15117df3ce883fbce5f026df8622383 Mon Sep 17 00:00:00 2001 From: Luis Bosque Date: Wed, 2 Oct 2013 12:32:25 +0200 Subject: [PATCH] Add function to read database host from redis --- lib/cartodb/carto_data.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/cartodb/carto_data.js b/lib/cartodb/carto_data.js index c817bfa8..ead03b3a 100644 --- a/lib/cartodb/carto_data.js +++ b/lib/cartodb/carto_data.js @@ -100,6 +100,26 @@ module.exports = function() { }); }; + /** + * Get the database host this particular subdomain/username + * + * @param req - standard express req object. importantly contains host information + * @param callback + */ + me.getDatabaseHost= function(req, callback) { + // strip subdomain from header host + var username = req.headers.host.split('.')[0]; + var redisKey = _.template(this.user_key, {username: username}); + + this.retrieve(this.user_metadata_db, redisKey, 'database_host', function(err, dbname) { + if ( err ) callback(err, null); + else if ( dbname === null ) { + callback(new Error("missing " + username + "'s dbuser in redis (try CARTODB/script/restore_redis)"), null); + } + else callback(err, dbname); + }); + }; + /** * Check the user map key for this particular subdomain/username *