Extract 'served-by-host-header' middleware and move it to api router

This commit is contained in:
Daniel García Aubert 2019-10-01 13:04:44 +02:00
parent 1bb1a350e0
commit 79143e3df6
3 changed files with 16 additions and 4 deletions

View File

@ -18,6 +18,8 @@ const CopyController = require('./copy_controller');
const JobController = require('./job_controller');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
module.exports = class ApiRouter {
constructor ({ redisPool, metadataBackend, statsClient, dataIngestionLogger }) {
@ -64,6 +66,7 @@ module.exports = class ApiRouter {
const apiRouter = router({ mergeParams: true });
apiRouter.use(cors());
apiRouter.use(servedByHostHeader());
this.queryController.route(apiRouter);
this.copyController.route(apiRouter);

View File

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

View File

@ -95,10 +95,6 @@ function App(statsClient) {
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
res.locals = res.locals || {};
if (global.settings.api_hostname) {
res.header('X-Served-By-Host', global.settings.api_hostname);
}
var profile = global.settings.useProfiler;
req.profiler = new Profiler({
profile: profile,