CartoDB-SQL-API/lib/api/sql/sql-router.js

75 lines
2.4 KiB
JavaScript
Raw Normal View History

'use strict';
const { Router: router } = require('express');
const UserDatabaseService = require('../../services/user-database-service');
const UserLimitsService = require('../../services/user-limits');
const socketTimeout = require('../middlewares/socket-timeout');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
2020-06-22 18:51:35 +08:00
const clientHeader = require('../middlewares/client-header');
const QueryController = require('./query-controller');
const CopyController = require('./copy-controller');
const JobController = require('./job-controller');
module.exports = class SqlRouter {
constructor ({ metadataBackend, statsClient, logger, jobService }) {
const userLimitsServiceOptions = {
limits: {
rateLimitsEnabled: global.settings.ratelimits.rateLimitsEnabled
}
};
const userDatabaseService = new UserDatabaseService(metadataBackend);
const userLimitsService = new UserLimitsService(metadataBackend, userLimitsServiceOptions);
2020-07-01 18:32:51 +08:00
this.queryController = new QueryController({
metadataBackend,
userDatabaseService,
statsClient,
userLimitsService,
logger
2020-07-01 18:32:51 +08:00
});
2020-07-01 18:32:51 +08:00
this.copyController = new CopyController({
metadataBackend,
userDatabaseService,
statsClient,
userLimitsService,
logger
2020-07-01 18:32:51 +08:00
});
2020-07-01 18:32:51 +08:00
this.jobController = new JobController({
metadataBackend,
userDatabaseService,
jobService,
statsClient,
userLimitsService,
logger
2020-07-01 18:32:51 +08:00
});
}
route (apiRouter, routes) {
routes.forEach(route => {
const sqlRouter = router({ mergeParams: true });
const paths = route.paths;
const middlewares = route.middlewares || [];
middlewares.forEach(middleware => sqlRouter.use(middleware()));
sqlRouter.use(socketTimeout());
sqlRouter.use(cors());
2020-06-22 18:51:35 +08:00
sqlRouter.use(clientHeader());
sqlRouter.use(servedByHostHeader());
this.queryController.route(sqlRouter);
this.copyController.route(sqlRouter);
this.jobController.route(sqlRouter);
paths.forEach(path => apiRouter.use(path, sqlRouter));
});
}
};