Create an api router isolated from heath checks

This commit is contained in:
Daniel García Aubert 2018-04-03 19:08:56 +02:00
parent 462ba62656
commit f1e421db05
4 changed files with 74 additions and 66 deletions

View File

@ -55,8 +55,6 @@ const TileTemplateController = require('./template/tile');
const AnalysesController = require('./analyses');
const ServerInfoController = require('./server-info');
module.exports = class ControllersFactory {
constructor ({ serverOptions, environmentOptions }) {
const redisOptions = Object.assign({}, environmentOptions.redis, {
@ -155,8 +153,6 @@ module.exports = class ControllersFactory {
templateMaps.on(eventType, namedMapProviderCache.invalidate.bind(namedMapProviderCache));
});
const versions = getAndValidateVersions(serverOptions);
this.mapConfigBasePath = serverOptions.base_url_mapconfig;
this.templateBasePath = serverOptions.base_url_templated;
@ -269,13 +265,12 @@ module.exports = class ControllersFactory {
authApi,
userLimitsApi
);
this.serverInfoController = new ServerInfoController(versions);
}
regist (app) {
regist (api) {
// FIXME: we need a better way to reset cache while running tests
if (process.env.NODE_ENV === 'test') {
app.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache;
api.layergroupAffectedTablesCache = this.layergroupAffectedTablesCache;
}
const mapRouter = router();
@ -289,7 +284,7 @@ module.exports = class ControllersFactory {
this.previewTemplateController.register(mapRouter);
this.analysesController.register(mapRouter);
app.use(this.mapConfigBasePath, mapRouter);
api.use(this.mapConfigBasePath, mapRouter);
const templateRouter = router();
@ -297,13 +292,7 @@ module.exports = class ControllersFactory {
this.tileTemplateController.register(templateRouter);
this.adminTemplateController.register(templateRouter);
app.use(this.templateBasePath, templateRouter);
const monitorRouter = router();
this.serverInfoController.register(monitorRouter);
app.use('/', monitorRouter);
api.use(this.templateBasePath, templateRouter);
}
};
@ -405,42 +394,3 @@ function createRendererFactory ({ redisPool, serverOptions, environmentOptions }
return rendererFactory;
}
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;
}

View File

@ -1,6 +1,7 @@
const _ = require('underscore');
const express = require('express');
const { mapnik } = require('windshaft');
const windshaft = require('windshaft');
const { mapnik } = windshaft;
const jsonReplacer = require('./utils/json-replacer');
@ -13,6 +14,7 @@ const cors = require('./middleware/cors');
const user = require('./middleware/user');
const ControllersFactory = require('./controllers/factory');
const ServerInfoController = require('./controllers/server-info');
const syntaxError = require('./middleware/syntax-error');
const errorMiddleware = require('./middleware/error-middleware');
@ -36,23 +38,37 @@ module.exports = function createServer (serverOptions) {
app.disable('etag');
app.set('json replacer', jsonReplacer());
app.use(logger(serverOptions));
app.use(bodyParser.json());
app.use(servedByHostHeader());
app.use(stats({
const api = express.Router();
api.use(logger(serverOptions));
api.use(bodyParser.json());
api.use(servedByHostHeader());
api.use(stats({
enabled: serverOptions.useProfiler,
statsClient: global.statsClient
}));
app.use(lzmaMiddleware());
app.use(cors());
app.use(user());
api.use(lzmaMiddleware());
api.use(cors());
api.use(user());
const controllers = new ControllersFactory({ serverOptions, environmentOptions: global.environment });
controllers.regist(app);
controllers.regist(api);
app.use(syntaxError());
app.use(errorMiddleware());
api.use(syntaxError());
api.use(errorMiddleware());
app.use('/', api);
const versions = getAndValidateVersions(serverOptions);
const serverInfoController = new ServerInfoController(versions);
serverInfoController.register(app);
// FIXME: we need a better way to reset cache while running tests
if (process.env.NODE_ENV === 'test') {
app.layergroupAffectedTablesCache = api.layergroupAffectedTablesCache;
}
return app;
};
@ -78,3 +94,42 @@ function bootstrapFonts(opts) {
function mapnikVersion(opts) {
return opts.grainstore.mapnik_version || mapnik.versions.mapnik;
}
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;
}

View File

@ -369,6 +369,7 @@ describe('tests from old api translated to multilayer', function() {
};
// reset internal cacheChannel cache
// FIXME: we need a better way to reset cache while running tests
server.layergroupAffectedTablesCache.cache.reset();
assert.response(server,

View File

@ -26,6 +26,7 @@ server.setMaxListeners(0);
var LayergroupToken = require('../../lib/cartodb/models/layergroup-token');
describe('template_api', function() {
// FIXME: we need a better way to reset cache while running tests
server.layergroupAffectedTablesCache.cache.reset();
var httpRendererResourcesServer;
@ -1056,6 +1057,7 @@ describe('template_api', function() {
assert.ok(cc);
assert.equal(cc, expectedCC);
// hack simulating restart...
// FIXME: we need a better way to reset cache while running tests
server.layergroupAffectedTablesCache.cache.reset(); // need to clean channel cache
var get_request = {
url: '/api/v1/map/' + layergroupid + ':cb1/0/0/0/1.json.torque?auth_token=valid1',