2018-04-03 01:02:31 +08:00
|
|
|
const _ = require('underscore');
|
2018-04-03 19:21:41 +08:00
|
|
|
const express = require('express');
|
2018-04-04 01:08:56 +08:00
|
|
|
const windshaft = require('windshaft');
|
|
|
|
const { mapnik } = windshaft;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
const jsonReplacer = require('./utils/json-replacer');
|
|
|
|
|
2018-04-09 22:18:30 +08:00
|
|
|
const ApiRouter = require('./api/api-router');
|
|
|
|
const ServerInfoController = require('./server-info-controller');
|
2018-03-29 01:58:30 +08:00
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
const StatsClient = require('./stats/client');
|
2015-07-05 05:09:00 +08:00
|
|
|
|
2018-04-03 01:02:31 +08:00
|
|
|
module.exports = function createServer (serverOptions) {
|
|
|
|
validateOptions(serverOptions);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-04-03 01:02:31 +08:00
|
|
|
// Make stats client globally accessible
|
|
|
|
global.statsClient = StatsClient.getInstance(serverOptions.statsd);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
serverOptions.grainstore.mapnik_version = mapnikVersion(serverOptions);
|
|
|
|
|
|
|
|
bootstrapFonts(serverOptions);
|
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
const app = express();
|
2018-03-29 01:37:31 +08:00
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
app.enable('jsonp callback');
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.disable('etag');
|
|
|
|
app.set('json replacer', jsonReplacer());
|
2018-04-03 18:26:35 +08:00
|
|
|
|
2018-04-06 01:42:20 +08:00
|
|
|
const apiRouter = new ApiRouter({ serverOptions, environmentOptions: global.environment });
|
|
|
|
apiRouter.register(app);
|
2018-04-04 01:16:37 +08:00
|
|
|
|
2018-04-04 01:08:56 +08:00
|
|
|
const versions = getAndValidateVersions(serverOptions);
|
2018-04-04 01:16:37 +08:00
|
|
|
|
2018-04-04 21:52:54 +08:00
|
|
|
const serverInfoController = new ServerInfoController(versions);
|
|
|
|
serverInfoController.register(app);
|
2017-09-21 17:46:31 +08:00
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
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
|
|
|
}
|
2016-06-06 18:07:43 +08:00
|
|
|
}
|
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
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 mapnikVersion(opts) {
|
|
|
|
return opts.grainstore.mapnik_version || mapnik.versions.mapnik;
|
|
|
|
}
|
2018-04-04 01:08:56 +08:00
|
|
|
|
|
|
|
function getAndValidateVersions(options) {
|
|
|
|
// jshint undef:false
|
|
|
|
var warn = console.warn.bind(console);
|
|
|
|
// jshint undef:true
|
|
|
|
|
|
|
|
var packageDefinition = require('../../package.json');
|
|
|
|
|
|
|
|
var declaredDependencies = packageDefinition.dependencies || {};
|
|
|
|
var installedDependenciesVersions = {
|
|
|
|
camshaft: require('camshaft').version,
|
|
|
|
grainstore: windshaft.grainstore.version(),
|
|
|
|
mapnik: windshaft.mapnik.versions.mapnik,
|
|
|
|
node_mapnik: windshaft.mapnik.version,
|
|
|
|
'turbo-carto': require('turbo-carto').version,
|
|
|
|
windshaft: windshaft.version,
|
|
|
|
windshaft_cartodb: packageDefinition.version
|
|
|
|
};
|
|
|
|
|
|
|
|
var dependenciesToValidate = ['camshaft', 'turbo-carto', 'windshaft'];
|
|
|
|
dependenciesToValidate.forEach(function(depName) {
|
|
|
|
var declaredDependencyVersion = declaredDependencies[depName];
|
|
|
|
var installedDependencyVersion = installedDependenciesVersions[depName];
|
|
|
|
if (declaredDependencyVersion !== installedDependencyVersion) {
|
|
|
|
warn(
|
|
|
|
'Dependency="%s" installed version="%s" does not match declared version="%s". Check your installation.',
|
|
|
|
depName, installedDependencyVersion, declaredDependencyVersion
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Be nice and warn if configured mapnik version is != installed mapnik version
|
|
|
|
if (windshaft.mapnik.versions.mapnik !== options.grainstore.mapnik_version) {
|
|
|
|
warn('WARNING: detected mapnik version (' + windshaft.mapnik.versions.mapnik + ')' +
|
|
|
|
' != configured mapnik version (' + options.grainstore.mapnik_version + ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
return installedDependenciesVersions;
|
|
|
|
}
|