Draft: be able to inject custom middlewares

This commit is contained in:
Daniel García Aubert 2019-10-01 17:27:46 +02:00
parent 07cf020923
commit 3dee09a5c5
9 changed files with 110 additions and 24 deletions

8
app.js
View File

@ -69,10 +69,10 @@ if (global.settings.log_filename) {
global.log4js.configure(log4jsConfig);
global.logger = global.log4js.getLogger();
// kick off controller
if ( ! global.settings.base_url ) {
global.settings.base_url = '/api/*';
if (!global.settings.routes) {
console.error('Missing environment paramenter "routes". Please review your configuration file.');
console.error("Available environments: " + availableEnvironments.join(', '));
process.exit(1);
}
const version = require("./package").version;

View File

@ -24,7 +24,8 @@ const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
module.exports = class ApiRouter {
constructor ({ redisPool, metadataBackend, statsClient, dataIngestionLogger }) {
constructor ({ routes, redisPool, metadataBackend, statsClient, dataIngestionLogger }) {
this.routes = routes;
this.statsClient = statsClient;
const userLimitsServiceOptions = {
@ -68,6 +69,10 @@ module.exports = class ApiRouter {
route (app) {
const apiRouter = router({ mergeParams: true });
const paths = this.routes.paths || [];
const middlewares = this.routes.middlewares || [];
middlewares.forEach(middleware => apiRouter.use(middleware()));
apiRouter.use(socketTimeout());
apiRouter.use(logger());
@ -79,6 +84,6 @@ module.exports = class ApiRouter {
this.copyController.route(apiRouter);
this.jobController.route(apiRouter);
app.use(`${global.settings.base_url}`, apiRouter);
paths.forEach(path => app.use(path, apiRouter));
}
};

View File

@ -3,12 +3,17 @@
const HealthCheckBackend = require('../monitoring/health_check');
module.exports = class HealthCheckController {
constructor () {
constructor ({ routes }) {
this.routes = routes;
this.healthCheckBackend = new HealthCheckBackend(global.settings.disabled_file);
}
route (app) {
app.get(`${global.settings.base_url}/health`, healthCheck({ healthCheckBackend: this.healthCheckBackend }));
const paths = this.routes.paths || [];
paths.forEach(path => app.get(`${path}/health`, healthCheck({
healthCheckBackend: this.healthCheckBackend
})));
}
};

View File

@ -5,8 +5,14 @@ const versions = {
};
module.exports = class VersionController {
constructor ({ routes }) {
this.routes = routes;
}
route (app) {
app.get(`${global.settings.base_url}/version`, version());
const paths = this.routes.paths || [];
paths.forEach(path => app.get(`${path}/version`, version()));
}
};

View File

@ -61,13 +61,27 @@ module.exports = function serverFactory (statsClient) {
const dataIngestionLogger = new Logger(global.settings.dataIngestionLogPath, 'data-ingestion');
app.dataIngestionLogger = dataIngestionLogger;
const healthCheckController = new HealthCheckController();
// FIXME: health controller should be atached to the main entry point: "/"
// instead of "/api/v1/"
const healthCheckController = new HealthCheckController({
routes: global.settings.routes
});
healthCheckController.route(app);
const versionController = new VersionController();
// FIXME: version controller should be atached to the main entry point: "/"
// instead of "/api/v1/"
const versionController = new VersionController({
routes: global.settings.routes
});
versionController.route(app);
const apiRouter = new ApiRouter({ redisPool, metadataBackend, statsClient, dataIngestionLogger });
const apiRouter = new ApiRouter({
routes: global.settings.routes,
redisPool,
metadataBackend,
statsClient,
dataIngestionLogger
});
apiRouter.route(app);
const isBatchProcess = process.argv.indexOf('--no-batch') === -1;

View File

@ -1,9 +1,23 @@
// Time in milliseconds to force GC cycle.
// Disable by using <=0 value.
module.exports.gc_interval = 10000;
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
module.exports.base_url = '(?:/api/:version|/user/:user/api/:version)';
module.exports.routes = {
paths: [
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
'/api/:version',
'/user/:user/api/:version',
],
// Attach middlewares at the begining of the req/res cycle
// to perform custom operations.
middlewares: [
function noop () {
return function noopMiddleware (req, res, next) {
next();
}
}
]
};
// If useProfiler is true every response will be served with an
// X-SQLAPI-Profile header containing elapsed timing for various
// steps taken for producing the response.

View File

@ -1,9 +1,23 @@
// Time in milliseconds to force GC cycle.
// Disable by using <=0 value.
module.exports.gc_interval = 10000;
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
module.exports.base_url = '(?:/api/:version|/user/:user/api/:version)';
module.exports.routes = {
paths: [
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
'/api/:version',
'/user/:user/api/:version',
],
// Attach middlewares at the begining of the req/res cycle
// to perform custom operations.
middlewares: [
function noop () {
return function noopMiddleware (req, res, next) {
next();
}
}
]
};
// If useProfiler is true every response will be served with an
// X-SQLAPI-Profile header containing elapsed timing for various
// steps taken for producing the response.

View File

@ -1,9 +1,23 @@
// Time in milliseconds to force GC cycle.
// Disable by using <=0 value.
module.exports.gc_interval = 10000;
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
module.exports.base_url = '(?:/api/:version|/user/:user/api/:version)';
module.exports.routes = {
paths: [
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
'/api/:version',
'/user/:user/api/:version',
],
// Attach middlewares at the begining of the req/res cycle
// to perform custom operations.
middlewares: [
function noop () {
return function noopMiddleware (req, res, next) {
next();
}
}
]
};
// If useProfiler is true every response will be served with an
// X-SQLAPI-Profile header containing elapsed timing for various
// steps taken for producing the response.

View File

@ -1,9 +1,23 @@
// Time in milliseconds to force GC cycle.
// Disable by using <=0 value.
module.exports.gc_interval = 10000;
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
module.exports.base_url = '(?:/api/:version|/user/:user/api/:version)';
module.exports.routes = {
paths: [
// In case the base_url has a :user param the username will be the one specified in the URL,
// otherwise it will fallback to extract the username from the host header.
'/api/:version',
'/user/:user/api/:version',
],
// Attach middlewares at the begining of the req/res cycle
// to perform custom operations.
middlewares: [
function noop () {
return function noopMiddleware (req, res, next) {
next();
}
}
]
};
// If useProfiler is true every response will be served with an
// X-SQLAPI-Profile header containing elapsed timing for various
// steps taken for producing the response.