2015-07-05 02:41:22 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var RedisPool = require('redis-mpool');
|
2015-07-05 05:44:39 +08:00
|
|
|
var cartodbRedis = require('cartodb-redis');
|
2015-07-05 02:41:22 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var step = require('step');
|
|
|
|
|
2015-07-09 02:51:36 +08:00
|
|
|
var controller = require('./controllers');
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
var SurrogateKeysCache = require('./cache/surrogate_keys_cache');
|
|
|
|
var NamedMapsCacheEntry = require('./cache/model/named_maps_entry');
|
|
|
|
var VarnishHttpCacheBackend = require('./cache/backend/varnish_http');
|
|
|
|
var FastlyCacheBackend = require('./cache/backend/fastly');
|
|
|
|
|
|
|
|
var windshaft = require('windshaft');
|
|
|
|
var mapnik = windshaft.mapnik;
|
|
|
|
|
|
|
|
var TemplateMaps = require('./backends/template_maps.js');
|
|
|
|
var QueryTablesApi = require('./api/query_tables_api');
|
2015-07-11 01:10:55 +08:00
|
|
|
var UserLimitsApi = require('./api/user_limits_api');
|
2015-07-13 21:05:03 +08:00
|
|
|
var AuthApi = require('./api/auth_api');
|
2015-07-14 19:40:41 +08:00
|
|
|
var LayergroupAffectedTablesCache = require('./cache/layergroup_affected_tables');
|
2015-07-05 02:41:22 +08:00
|
|
|
var PgQueryRunner = require('./backends/pg_query_runner');
|
|
|
|
var PgConnection = require('./backends/pg_connection');
|
|
|
|
|
|
|
|
var CdbRequest = require('./models/cdb_request');
|
|
|
|
var cdbRequest = new CdbRequest();
|
|
|
|
|
|
|
|
var LZMA = require('lzma').LZMA;
|
|
|
|
// Whitelist query parameters and attach format
|
|
|
|
var REQUEST_QUERY_PARAMS_WHITELIST = [
|
|
|
|
'config',
|
|
|
|
'map_key',
|
|
|
|
'api_key',
|
|
|
|
'auth_token',
|
|
|
|
'callback'
|
|
|
|
];
|
|
|
|
|
|
|
|
var lzmaWorker = new LZMA();
|
|
|
|
|
|
|
|
var timeoutErrorTilePath = __dirname + '/../../assets/render-timeout-fallback.png';
|
|
|
|
var timeoutErrorTile = require('fs').readFileSync(timeoutErrorTilePath, {encoding: null});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(serverOptions) {
|
2015-07-05 05:09:00 +08:00
|
|
|
// Make stats client globally accessible
|
|
|
|
global.statsClient = windshaft.stats.Client.getInstance(serverOptions.statsd);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-07 18:36:39 +08:00
|
|
|
var redisPool = new RedisPool(_.defaults(global.environment.redis, {
|
|
|
|
name: 'windshaft:server',
|
|
|
|
unwatchOnRelease: false,
|
|
|
|
noReadyCheck: true
|
|
|
|
}));
|
2015-07-05 05:09:00 +08:00
|
|
|
|
|
|
|
redisPool.on('status', function(status) {
|
|
|
|
var keyPrefix = 'windshaft.redis-pool.' + status.name + '.db' + status.db + '.';
|
|
|
|
global.statsClient.gauge(keyPrefix + 'count', status.count);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'unused', status.unused);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'waiting', status.waiting);
|
|
|
|
});
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-05 05:44:39 +08:00
|
|
|
var metadataBackend = cartodbRedis({pool: redisPool});
|
|
|
|
var pgConnection = new PgConnection(metadataBackend);
|
2015-07-05 02:41:22 +08:00
|
|
|
var pgQueryRunner = new PgQueryRunner(pgConnection);
|
|
|
|
var queryTablesApi = new QueryTablesApi(pgQueryRunner);
|
2015-07-11 01:10:55 +08:00
|
|
|
var userLimitsApi = new UserLimitsApi(metadataBackend, {
|
|
|
|
limits: {
|
|
|
|
cacheOnTimeout: serverOptions.renderer.mapnik.limits.cacheOnTimeout || false,
|
|
|
|
render: serverOptions.renderer.mapnik.limits.render || 0
|
|
|
|
}
|
|
|
|
});
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
var templateMaps = new TemplateMaps(redisPool, {
|
|
|
|
max_user_templates: global.environment.maxUserTemplates
|
|
|
|
});
|
|
|
|
|
|
|
|
var surrogateKeysCacheBackends = [];
|
|
|
|
|
|
|
|
if (serverOptions.varnish_purge_enabled) {
|
|
|
|
surrogateKeysCacheBackends.push(
|
|
|
|
new VarnishHttpCacheBackend(serverOptions.varnish_host, serverOptions.varnish_http_port)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverOptions.fastly &&
|
|
|
|
!!serverOptions.fastly.enabled && !!serverOptions.fastly.apiKey && !!serverOptions.fastly.serviceId) {
|
|
|
|
surrogateKeysCacheBackends.push(
|
|
|
|
new FastlyCacheBackend(serverOptions.fastly.apiKey, serverOptions.fastly.serviceId)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var surrogateKeysCache = new SurrogateKeysCache(surrogateKeysCacheBackends);
|
|
|
|
|
|
|
|
function invalidateNamedMap (owner, templateName) {
|
|
|
|
var startTime = Date.now();
|
|
|
|
surrogateKeysCache.invalidate(new NamedMapsCacheEntry(owner, templateName), function(err) {
|
|
|
|
var logMessage = JSON.stringify({
|
|
|
|
username: owner,
|
|
|
|
type: 'named_map_invalidation',
|
|
|
|
elapsed: Date.now() - startTime,
|
|
|
|
error: !!err ? JSON.stringify(err.message) : undefined
|
|
|
|
});
|
|
|
|
if (err) {
|
|
|
|
console.warn(logMessage);
|
|
|
|
} else {
|
|
|
|
console.info(logMessage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
['update', 'delete'].forEach(function(eventType) {
|
|
|
|
templateMaps.on(eventType, invalidateNamedMap);
|
|
|
|
});
|
|
|
|
|
|
|
|
serverOptions.grainstore.mapnik_version = mapnikVersion(serverOptions);
|
|
|
|
|
|
|
|
validateOptions(serverOptions);
|
|
|
|
|
|
|
|
bootstrapFonts(serverOptions);
|
|
|
|
|
|
|
|
// initialize express server
|
|
|
|
var app = bootstrap(serverOptions);
|
2015-07-05 03:33:31 +08:00
|
|
|
// Extend windshaft with all the elements of the options object
|
|
|
|
_.extend(app, serverOptions);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-05 05:18:09 +08:00
|
|
|
var mapStore = new windshaft.storage.MapStore({
|
2015-07-05 02:41:22 +08:00
|
|
|
pool: redisPool,
|
|
|
|
expire_time: serverOptions.grainstore.default_layergroup_ttl
|
|
|
|
});
|
|
|
|
|
|
|
|
var onTileErrorStrategy;
|
|
|
|
if (global.environment.enabledFeatures.onTileErrorStrategy !== false) {
|
|
|
|
onTileErrorStrategy = function onTileErrorStrategy$TimeoutTile(err, tile, headers, stats, format, callback) {
|
|
|
|
if (err && err.message === 'Render timed out' && format === 'png') {
|
|
|
|
return callback(null, timeoutErrorTile, { 'Content-Type': 'image/png' }, {});
|
|
|
|
} else {
|
|
|
|
return callback(err, tile, headers, stats);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var rendererFactory = new windshaft.renderer.Factory({
|
|
|
|
onTileErrorStrategy: onTileErrorStrategy,
|
|
|
|
mapnik: {
|
|
|
|
redisPool: redisPool,
|
|
|
|
grainstore: serverOptions.grainstore,
|
2015-07-05 05:44:39 +08:00
|
|
|
mapnik: serverOptions.renderer.mapnik
|
2015-07-05 02:41:22 +08:00
|
|
|
},
|
|
|
|
http: serverOptions.renderer.http
|
|
|
|
});
|
|
|
|
|
|
|
|
// initialize render cache
|
|
|
|
var rendererCacheOpts = _.defaults(serverOptions.renderCache || {}, {
|
|
|
|
ttl: 60000, // 60 seconds TTL by default
|
2015-07-10 18:30:52 +08:00
|
|
|
statsInterval: 60000 // reports stats every milliseconds defined here
|
2015-07-05 02:41:22 +08:00
|
|
|
});
|
2015-07-10 18:30:52 +08:00
|
|
|
var rendererCache = new windshaft.cache.RendererCache(rendererFactory, rendererCacheOpts);
|
2015-07-09 02:51:36 +08:00
|
|
|
|
|
|
|
var attributesBackend = new windshaft.backend.Attributes(rendererCache, mapStore);
|
|
|
|
var previewBackend = new windshaft.backend.Preview(rendererCache);
|
|
|
|
var tileBackend = new windshaft.backend.Tile(rendererCache);
|
|
|
|
var mapValidatorBackend = new windshaft.backend.MapValidator(tileBackend, attributesBackend);
|
|
|
|
var mapBackend = new windshaft.backend.Map(rendererCache, mapStore, mapValidatorBackend);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-14 19:40:41 +08:00
|
|
|
var layergroupAffectedTablesCache = new LayergroupAffectedTablesCache();
|
|
|
|
app.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
|
|
|
|
|
2015-07-13 21:05:03 +08:00
|
|
|
var authApi = new AuthApi(pgConnection, metadataBackend, mapStore, templateMaps);
|
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
app.findStatusCode = function(err) {
|
|
|
|
var statusCode;
|
|
|
|
if ( err.http_status ) {
|
|
|
|
statusCode = err.http_status;
|
|
|
|
} else {
|
|
|
|
statusCode = statusFromErrorMessage('' + err);
|
|
|
|
}
|
|
|
|
return statusCode;
|
|
|
|
};
|
|
|
|
|
2015-07-08 21:50:59 +08:00
|
|
|
var TablesExtentApi = require('./api/tables_extent_api');
|
|
|
|
var tablesExtentApi = new TablesExtentApi(pgQueryRunner);
|
|
|
|
|
2015-07-09 02:51:36 +08:00
|
|
|
/*******************************************************************************************************************
|
|
|
|
* Routing
|
|
|
|
******************************************************************************************************************/
|
|
|
|
|
|
|
|
app.all('*', function(req, res, next) {
|
|
|
|
req.context.user = cdbRequest.userByReq(req);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
new controller.Layergroup(
|
2015-07-09 02:51:36 +08:00
|
|
|
app,
|
|
|
|
mapStore,
|
|
|
|
tileBackend,
|
|
|
|
previewBackend,
|
2015-07-11 01:10:55 +08:00
|
|
|
attributesBackend,
|
2015-07-14 19:40:41 +08:00
|
|
|
surrogateKeysCache,
|
2015-07-14 17:55:49 +08:00
|
|
|
userLimitsApi,
|
2015-07-14 19:40:41 +08:00
|
|
|
queryTablesApi,
|
|
|
|
layergroupAffectedTablesCache
|
2015-07-09 02:51:36 +08:00
|
|
|
).register(app);
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
new controller.Map(
|
2015-07-09 02:51:36 +08:00
|
|
|
app,
|
2015-07-09 20:39:25 +08:00
|
|
|
pgConnection,
|
2015-07-09 02:51:36 +08:00
|
|
|
templateMaps,
|
|
|
|
mapBackend,
|
2015-07-10 17:24:32 +08:00
|
|
|
metadataBackend,
|
|
|
|
queryTablesApi,
|
2015-07-11 01:10:55 +08:00
|
|
|
surrogateKeysCache,
|
2015-07-14 19:40:41 +08:00
|
|
|
userLimitsApi,
|
|
|
|
layergroupAffectedTablesCache
|
2015-07-10 17:24:32 +08:00
|
|
|
).register(app);
|
|
|
|
|
|
|
|
new controller.NamedMaps(
|
|
|
|
app,
|
|
|
|
pgConnection,
|
|
|
|
templateMaps,
|
2015-07-09 19:37:00 +08:00
|
|
|
tileBackend,
|
2015-07-09 02:51:36 +08:00
|
|
|
previewBackend,
|
|
|
|
surrogateKeysCache,
|
2015-07-11 01:10:55 +08:00
|
|
|
tablesExtentApi,
|
2015-07-15 02:10:55 +08:00
|
|
|
userLimitsApi,
|
|
|
|
queryTablesApi
|
2015-07-09 02:51:36 +08:00
|
|
|
).register(app);
|
|
|
|
|
2015-07-13 21:05:03 +08:00
|
|
|
new controller.NamedMapsAdmin(app, templateMaps, authApi).register(app);
|
2015-07-09 02:51:36 +08:00
|
|
|
|
|
|
|
new controller.ServerInfo().register(app);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
/*******************************************************************************************************************
|
|
|
|
* END Routing
|
|
|
|
******************************************************************************************************************/
|
|
|
|
|
|
|
|
// temporary measure until we upgrade to newer version expressjs so we can check err.status
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
if (err) {
|
|
|
|
if (err.name === 'SyntaxError') {
|
|
|
|
app.sendError(res, { errors: [err.name + ': ' + err.message] }, 400, 'JSON', err);
|
|
|
|
} else {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.sendResponse = function(res, args) {
|
|
|
|
var req = res.req;
|
|
|
|
|
2015-07-14 17:55:49 +08:00
|
|
|
if (global.environment && global.environment.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.environment.api_hostname);
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-14 17:55:49 +08:00
|
|
|
if (req && req.params && req.params.dbhost) {
|
|
|
|
res.header('X-Served-By-DB-Host', req.params.dbhost);
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-14 17:55:49 +08:00
|
|
|
if ( req && req.profiler ) {
|
|
|
|
res.header('X-Tiler-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-14 17:55:49 +08:00
|
|
|
// res.send(body|status[, headers|status[, status]])
|
|
|
|
res.send.apply(res, args);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2015-07-14 17:55:49 +08:00
|
|
|
if ( req && req.profiler ) {
|
|
|
|
try {
|
|
|
|
// May throw due to dns, see
|
|
|
|
// See http://github.com/CartoDB/Windshaft/issues/166
|
|
|
|
req.profiler.sendStats();
|
|
|
|
} catch (err) {
|
|
|
|
console.error("error sending profiling stats: " + err);
|
|
|
|
}
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
app.sendError = function(res, err, statusCode, label, tolog) {
|
|
|
|
res._windshaftStatusCode = statusCode;
|
|
|
|
|
|
|
|
var olabel = '[';
|
|
|
|
if ( label ) {
|
|
|
|
olabel += label + ' ';
|
|
|
|
}
|
|
|
|
olabel += 'ERROR]';
|
|
|
|
if ( ! tolog ) {
|
|
|
|
tolog = err;
|
|
|
|
}
|
|
|
|
var log_msg = olabel + " -- " + statusCode + ": " + tolog;
|
|
|
|
//if ( tolog.stack ) log_msg += "\n" + tolog.stack;
|
|
|
|
console.error(log_msg); // use console.log for statusCode != 500 ?
|
|
|
|
// If a callback was requested, force status to 200
|
|
|
|
if ( res.req ) {
|
|
|
|
// NOTE: res.req can be undefined when we fake a call to
|
|
|
|
// ourself from POST to /layergroup
|
|
|
|
if ( res.req.query.callback ) {
|
|
|
|
statusCode = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Strip connection info, if any
|
|
|
|
// See https://github.com/CartoDB/Windshaft/issues/173
|
|
|
|
err = JSON.stringify(err);
|
|
|
|
err = err.replace(/Connection string: '[^']*'\\n/, '');
|
|
|
|
// See https://travis-ci.org/CartoDB/Windshaft/jobs/20703062#L1644
|
|
|
|
err = err.replace(/is the server.*encountered/im, 'encountered');
|
|
|
|
err = JSON.parse(err);
|
|
|
|
|
|
|
|
app.sendResponse(res, [err, statusCode]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// jshint maxcomplexity:10
|
|
|
|
/**
|
|
|
|
* Whitelist input and get database name & default geometry type from
|
|
|
|
* subdomain/user metadata held in CartoDB Redis
|
|
|
|
* @param req - standard express request obj. Should have host & table
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
app.req2params = function(req, callback){
|
|
|
|
|
|
|
|
if ( req.query.lzma ) {
|
|
|
|
|
|
|
|
// Decode (from base64)
|
|
|
|
var lzma = new Buffer(req.query.lzma, 'base64')
|
|
|
|
.toString('binary')
|
|
|
|
.split('')
|
|
|
|
.map(function(c) {
|
|
|
|
return c.charCodeAt(0) - 128;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Decompress
|
|
|
|
lzmaWorker.decompress(
|
|
|
|
lzma,
|
|
|
|
function(result) {
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.done('lzma');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
delete req.query.lzma;
|
|
|
|
_.extend(req.query, JSON.parse(result));
|
|
|
|
app.req2params(req, callback);
|
|
|
|
} catch (err) {
|
|
|
|
callback(new Error('Error parsing lzma as JSON: ' + err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req.query = _.pick(req.query, REQUEST_QUERY_PARAMS_WHITELIST);
|
|
|
|
req.params = _.extend({}, req.params); // shuffle things as request is a strange array/object
|
|
|
|
|
2015-07-08 21:34:46 +08:00
|
|
|
var user = req.context.user;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
if ( req.params.token ) {
|
2015-07-05 05:32:19 +08:00
|
|
|
// Token might match the following patterns:
|
|
|
|
// - {user}@{tpl_id}@{token}:{cache_buster}
|
2015-07-05 02:41:22 +08:00
|
|
|
//console.log("Request parameters include token " + req.params.token);
|
|
|
|
var tksplit = req.params.token.split(':');
|
|
|
|
req.params.token = tksplit[0];
|
|
|
|
if ( tksplit.length > 1 ) {
|
|
|
|
req.params.cache_buster= tksplit[1];
|
|
|
|
}
|
|
|
|
tksplit = req.params.token.split('@');
|
|
|
|
if ( tksplit.length > 1 ) {
|
|
|
|
req.params.signer = tksplit.shift();
|
|
|
|
if ( ! req.params.signer ) {
|
|
|
|
req.params.signer = user;
|
|
|
|
}
|
|
|
|
else if ( req.params.signer !== user ) {
|
|
|
|
var err = new Error(
|
|
|
|
'Cannot use map signature of user "' + req.params.signer + '" on db of user "' + user + '"'
|
|
|
|
);
|
|
|
|
err.http_status = 403;
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( tksplit.length > 1 ) {
|
|
|
|
/*var template_hash = */tksplit.shift(); // unused
|
|
|
|
}
|
|
|
|
req.params.token = tksplit.shift();
|
|
|
|
//console.log("Request for token " + req.params.token + " with signature from " + req.params.signer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring all query values onto req.params object
|
|
|
|
_.extend(req.params, req.query);
|
|
|
|
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.done('req2params.setup');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getPrivacy(){
|
2015-07-13 21:05:03 +08:00
|
|
|
authApi.authorize(req, this);
|
2015-07-05 02:41:22 +08:00
|
|
|
},
|
2015-07-13 23:18:50 +08:00
|
|
|
function validateAuthorization(err, authorized) {
|
2015-07-05 02:41:22 +08:00
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.done('authorize');
|
|
|
|
}
|
|
|
|
assert.ifError(err);
|
|
|
|
if(!authorized) {
|
|
|
|
err = new Error("Sorry, you are unauthorized (permission denied)");
|
|
|
|
err.http_status = 403;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function getDatabase(err){
|
|
|
|
assert.ifError(err);
|
|
|
|
pgConnection.setDBConn(user, req.params, this);
|
|
|
|
},
|
|
|
|
function finishSetup(err) {
|
2015-07-13 23:18:50 +08:00
|
|
|
if ( err ) {
|
|
|
|
return callback(err, req);
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
// Add default database connection parameters
|
|
|
|
// if none given
|
|
|
|
_.defaults(req.params, {
|
|
|
|
dbuser: global.environment.postgres.user,
|
|
|
|
dbpassword: global.environment.postgres.password,
|
|
|
|
dbhost: global.environment.postgres.host,
|
|
|
|
dbport: global.environment.postgres.port
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null, req);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return app;
|
|
|
|
};
|
|
|
|
|
|
|
|
function validateOptions(opts) {
|
2015-07-10 17:24:32 +08:00
|
|
|
if (!_.isString(opts.base_url) || !_.isString(opts.base_url_mapconfig) || !_.isString(opts.base_url_templated)) {
|
|
|
|
throw new Error("Must initialise server with: 'base_url'/'base_url_mapconfig'/'base_url_templated' URLs");
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Be nice and warn if configured mapnik version is != instaled mapnik version
|
|
|
|
if (mapnik.versions.mapnik !== opts.grainstore.mapnik_version) {
|
|
|
|
console.warn('WARNING: detected mapnik version (' + mapnik.versions.mapnik + ')' +
|
|
|
|
' != configured mapnik version (' + opts.grainstore.mapnik_version + ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bootstrapFonts(opts) {
|
|
|
|
// Set carto renderer configuration for MMLStore
|
|
|
|
opts.grainstore.carto_env = opts.grainstore.carto_env || {};
|
|
|
|
var cenv = opts.grainstore.carto_env;
|
|
|
|
cenv.validation_data = cenv.validation_data || {};
|
|
|
|
if ( ! cenv.validation_data.fonts ) {
|
|
|
|
mapnik.register_system_fonts();
|
|
|
|
mapnik.register_default_fonts();
|
|
|
|
cenv.validation_data.fonts = _.keys(mapnik.fontFiles());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bootstrap(opts) {
|
|
|
|
var app;
|
|
|
|
if (_.isObject(opts.https)) {
|
|
|
|
// use https if possible
|
|
|
|
app = express.createServer(opts.https);
|
|
|
|
} else {
|
|
|
|
// fall back to http by default
|
|
|
|
app = express.createServer();
|
|
|
|
}
|
|
|
|
app.enable('jsonp callback');
|
|
|
|
app.use(express.bodyParser());
|
|
|
|
|
2015-07-10 17:25:20 +08:00
|
|
|
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
|
2015-07-05 02:41:22 +08:00
|
|
|
req.context = req.context || {};
|
|
|
|
req.profiler = new windshaft.stats.Profiler({
|
|
|
|
statsd_client: global.statsClient,
|
|
|
|
profile: opts.useProfiler
|
|
|
|
});
|
2015-07-10 17:25:20 +08:00
|
|
|
res.removeHeader('x-powered-by');
|
2015-07-05 02:41:22 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
setupLogger(app, opts);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupLogger(app, opts) {
|
|
|
|
if (opts.log_format) {
|
|
|
|
var loggerOpts = {
|
|
|
|
// Allowing for unbuffered logging is mainly
|
|
|
|
// used to avoid hanging during unit testing.
|
|
|
|
// TODO: provide an explicit teardown function instead,
|
|
|
|
// releasing any event handler or timer set by
|
|
|
|
// this component.
|
|
|
|
buffer: !opts.unbuffered_logging,
|
|
|
|
// optional log format
|
|
|
|
format: opts.log_format
|
|
|
|
};
|
|
|
|
if (global.log4js) {
|
|
|
|
app.use(global.log4js.connectLogger(global.log4js.getLogger(), _.defaults(loggerOpts, {level: 'info'})));
|
|
|
|
} else {
|
|
|
|
app.use(express.logger(loggerOpts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function statusFromErrorMessage(errMsg) {
|
|
|
|
// Find an appropriate statusCode based on message
|
|
|
|
var statusCode = 400;
|
|
|
|
if ( -1 !== errMsg.indexOf('permission denied') ) {
|
|
|
|
statusCode = 403;
|
|
|
|
}
|
|
|
|
else if ( -1 !== errMsg.indexOf('authentication failed') ) {
|
|
|
|
statusCode = 403;
|
|
|
|
}
|
|
|
|
else if (errMsg.match(/Postgis Plugin.*[\s|\n].*column.*does not exist/)) {
|
|
|
|
statusCode = 400;
|
|
|
|
}
|
|
|
|
else if ( -1 !== errMsg.indexOf('does not exist') ) {
|
|
|
|
if ( -1 !== errMsg.indexOf(' role ') ) {
|
|
|
|
statusCode = 403; // role 'xxx' does not exist
|
|
|
|
} else {
|
|
|
|
statusCode = 404;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapnikVersion(opts) {
|
|
|
|
return opts.grainstore.mapnik_version || mapnik.versions.mapnik;
|
|
|
|
}
|