2011-10-13 19:17:00 +08:00
|
|
|
|
|
|
|
var _ = require('underscore')
|
|
|
|
, Step = require('step')
|
2011-10-13 22:20:29 +08:00
|
|
|
, Windshaft = require('windshaft')
|
2011-12-09 08:32:32 +08:00
|
|
|
, Cache = require('./cache_validator');
|
2011-10-13 19:17:00 +08:00
|
|
|
|
|
|
|
var CartodbWindshaft = function(serverOptions) {
|
|
|
|
|
2011-12-01 02:59:28 +08:00
|
|
|
if(serverOptions.cache_enabled) {
|
2011-12-01 03:00:41 +08:00
|
|
|
console.log("cache invalidation enabled, varnish on ", serverOptions.varnish_host, ' ', serverOptions.varnish_port);
|
2011-12-01 02:59:28 +08:00
|
|
|
Cache.init(serverOptions.varnish_host, serverOptions.varnish_port);
|
|
|
|
serverOptions.afterStateChange = function(req, data, callback) {
|
2012-05-03 02:32:54 +08:00
|
|
|
Cache.invalidate_db(req.params.dbname, req.params.table);
|
2011-12-01 02:59:28 +08:00
|
|
|
callback(null, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 22:15:41 +08:00
|
|
|
serverOptions.beforeStateChange = function(req, callback) {
|
|
|
|
var err = null;
|
2012-09-03 20:54:23 +08:00
|
|
|
if ( ! req.hasOwnProperty('dbuser') ) {
|
2012-08-14 22:15:41 +08:00
|
|
|
err = new Error("map state cannot be changed by unauthenticated request!");
|
|
|
|
}
|
|
|
|
callback(err, req);
|
|
|
|
}
|
|
|
|
|
2011-10-13 22:20:29 +08:00
|
|
|
// boot
|
|
|
|
var ws = new Windshaft.Server(serverOptions);
|
2011-11-30 04:19:10 +08:00
|
|
|
|
2011-10-13 22:20:29 +08:00
|
|
|
/**
|
|
|
|
* Helper to allow access to the layer to be used in the maps infowindow popup.
|
|
|
|
*/
|
|
|
|
ws.get(serverOptions.base_url + '/infowindow', function(req, res){
|
2012-08-14 20:48:10 +08:00
|
|
|
ws.doCORS(res);
|
2011-10-13 22:20:29 +08:00
|
|
|
Step(
|
|
|
|
function(){
|
|
|
|
serverOptions.getInfowindow(req, this);
|
|
|
|
},
|
|
|
|
function(err, data){
|
|
|
|
if (err){
|
2012-08-15 02:00:26 +08:00
|
|
|
res.send({error: err.message}, 500);
|
2011-10-13 22:20:29 +08:00
|
|
|
} else {
|
|
|
|
res.send({infowindow: data}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2011-10-13 19:17:00 +08:00
|
|
|
|
2011-12-01 02:59:28 +08:00
|
|
|
|
2011-10-13 22:20:29 +08:00
|
|
|
/**
|
|
|
|
* Helper to allow access to metadata to be used in embedded maps.
|
|
|
|
*/
|
|
|
|
ws.get(serverOptions.base_url + '/map_metadata', function(req, res){
|
2012-08-14 20:48:10 +08:00
|
|
|
ws.doCORS(res);
|
2011-10-13 22:20:29 +08:00
|
|
|
Step(
|
|
|
|
function(){
|
|
|
|
serverOptions.getMapMetadata(req, this);
|
|
|
|
},
|
|
|
|
function(err, data){
|
|
|
|
if (err){
|
2011-11-22 06:29:35 +08:00
|
|
|
res.send(err.message, 500);
|
2011-10-13 22:20:29 +08:00
|
|
|
} else {
|
|
|
|
res.send({map_metadata: data}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2011-10-13 19:17:00 +08:00
|
|
|
|
2012-05-03 02:32:54 +08:00
|
|
|
/**
|
|
|
|
* Helper API to allow per table tile cache (and sql cache) to be invalidated remotely.
|
|
|
|
* TODO: Move?
|
|
|
|
*/
|
|
|
|
ws.del(serverOptions.base_url + '/flush_cache', function(req, res){
|
2012-08-14 20:48:10 +08:00
|
|
|
ws.doCORS(res);
|
2012-05-03 02:32:54 +08:00
|
|
|
Step(
|
|
|
|
function(){
|
|
|
|
serverOptions.flushCache(req, Cache, this);
|
|
|
|
},
|
|
|
|
function(err, data){
|
|
|
|
if (err){
|
|
|
|
res.send(500);
|
|
|
|
} else {
|
|
|
|
res.send({status: 'ok'}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2011-10-13 22:20:29 +08:00
|
|
|
return ws;
|
2011-10-13 19:17:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CartodbWindshaft;
|