Extract middleware served by host header

This commit is contained in:
Daniel García Aubert 2018-03-26 19:53:33 +02:00
parent 59db640d0d
commit cb488cbde8
3 changed files with 13 additions and 9 deletions

2
app.js
View File

@ -100,8 +100,6 @@ if ( global.environment.log_filename ) {
global.log4js.configure(log4jsConfig);
global.logger = global.log4js.getLogger();
global.environment.api_hostname = require('os').hostname().split('.')[0];
// Include cartodb_windshaft only _after_ the "global" variable is set
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/28
var cartodbWindshaft = require('./lib/cartodb/server');

View File

@ -0,0 +1,11 @@
const os = require('os');
module.exports = function servedByHostHeader () {
const hostname = os.hostname().split('.')[0];
return function servedByHostHeaderMiddleware (req, res, next) {
res.set('X-Served-By-Host', hostname);
next();
};
};

View File

@ -50,6 +50,7 @@ var StatsBackend = require('./backends/stats');
const lzmaMiddleware = require('./middleware/lzma');
const errorMiddleware = require('./middleware/error-middleware');
const servedByHostHeader = require('./middleware/served-by-host-header');
module.exports = function(serverOptions) {
// Make stats client globally accessible
@ -364,13 +365,7 @@ function bootstrap(opts) {
app.use(bodyParser.json());
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
if (global.environment && global.environment.api_hostname) {
res.set('X-Served-By-Host', global.environment.api_hostname);
}
next();
});
app.use(servedByHostHeader());
app.use(stats({
enabled: opts.useProfiler,