Extract cache-channel and surrogate-key middlewares

This commit is contained in:
Daniel García Aubert 2019-07-26 18:56:54 +02:00
parent c48b8b2f69
commit ae4b3a9f4f
3 changed files with 32 additions and 7 deletions

View File

@ -20,6 +20,8 @@ const affectedTables = require('../middlewares/affected-tables');
const accessValidator = require('../middlewares/access-validator'); const accessValidator = require('../middlewares/access-validator');
const queryMayWrite = require('../middlewares/query-may-write'); const queryMayWrite = require('../middlewares/query-may-write');
const cacheControl = require('../middlewares/cache-control'); const cacheControl = require('../middlewares/cache-control');
const cacheChannel = require('../middlewares/cache-channel');
const surrogateKey = require('../middlewares/surrogate-key');
function QueryController(metadataBackend, userDatabaseService, statsd_client, userLimitsService) { function QueryController(metadataBackend, userDatabaseService, statsd_client, userLimitsService) {
this.metadataBackend = metadataBackend; this.metadataBackend = metadataBackend;
@ -48,6 +50,8 @@ QueryController.prototype.route = function (app) {
accessValidator(), accessValidator(),
queryMayWrite(), queryMayWrite(),
cacheControl(), cacheControl(),
cacheChannel(),
surrogateKey(),
this.handleQuery.bind(this), this.handleQuery.bind(this),
errorMiddleware() errorMiddleware()
]; ];
@ -92,13 +96,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
res.header("Content-Disposition", getContentDisposition(formatter, filename, useInline)); res.header("Content-Disposition", getContentDisposition(formatter, filename, useInline));
res.header("Content-Type", formatter.getContentType()); res.header("Content-Type", formatter.getContentType());
// Only set an X-Cache-Channel for responses we want Varnish to cache.
var skipNotUpdatedAtTables = true;
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
res.header('X-Cache-Channel', affectedTables.getCacheChannel(skipNotUpdatedAtTables));
res.header('Surrogate-Key', affectedTables.key(skipNotUpdatedAtTables).join(' '));
}
if(!!affectedTables) { if(!!affectedTables) {
res.header('Last-Modified', res.header('Last-Modified',
new Date(affectedTables.getLastUpdatedAt(Number(new Date()))).toUTCString()); new Date(affectedTables.getLastUpdatedAt(Number(new Date()))).toUTCString());

View File

@ -0,0 +1,14 @@
'use strict';
module.exports = function cacheChannel () {
return function cacheChannelMiddleware (req, res, next) {
const { affectedTables, mayWrite } = res.locals;
const skipNotUpdatedAtTables = true;
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
res.header('X-Cache-Channel', affectedTables.getCacheChannel(skipNotUpdatedAtTables));
}
next();
};
};

View File

@ -0,0 +1,14 @@
'use strict';
module.exports = function surrogateKey () {
return function surrogateKeyMiddleware (req, res, next) {
const { affectedTables, mayWrite } = res.locals;
const skipNotUpdatedAtTables = true;
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
res.header('Surrogate-Key', affectedTables.key(skipNotUpdatedAtTables).join(' '));
}
next();
};
};