2019-10-02 22:02:13 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Router: router } = require('express');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const UserDatabaseService = require('../../services/user-database-service');
|
|
|
|
const UserLimitsService = require('../../services/user-limits');
|
2019-10-02 22:02:13 +08:00
|
|
|
|
|
|
|
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');
|
2019-10-02 22:02:13 +08:00
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const QueryController = require('./query-controller');
|
|
|
|
const CopyController = require('./copy-controller');
|
|
|
|
const JobController = require('./job-controller');
|
2019-10-02 22:02:13 +08:00
|
|
|
|
|
|
|
module.exports = class SqlRouter {
|
2020-06-30 23:42:59 +08:00
|
|
|
constructor ({ metadataBackend, statsClient, logger, jobService }) {
|
2019-10-02 22:02:13 +08:00
|
|
|
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({
|
2019-10-02 22:02:13 +08:00
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
|
|
|
statsClient,
|
2020-07-01 18:29:51 +08:00
|
|
|
userLimitsService,
|
|
|
|
logger
|
2020-07-01 18:32:51 +08:00
|
|
|
});
|
2019-10-02 22:02:13 +08:00
|
|
|
|
2020-07-01 18:32:51 +08:00
|
|
|
this.copyController = new CopyController({
|
2019-10-02 22:02:13 +08:00
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
2020-07-01 18:29:51 +08:00
|
|
|
statsClient,
|
|
|
|
userLimitsService,
|
|
|
|
logger
|
2020-07-01 18:32:51 +08:00
|
|
|
});
|
2019-10-02 22:02:13 +08:00
|
|
|
|
2020-07-01 18:32:51 +08:00
|
|
|
this.jobController = new JobController({
|
2019-10-02 22:02:13 +08:00
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
|
|
|
jobService,
|
|
|
|
statsClient,
|
2020-07-01 18:29:51 +08:00
|
|
|
userLimitsService,
|
|
|
|
logger
|
2020-07-01 18:32:51 +08:00
|
|
|
});
|
2019-10-02 22:02:13 +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());
|
2019-10-02 22:02:13 +08:00
|
|
|
sqlRouter.use(servedByHostHeader());
|
|
|
|
|
|
|
|
this.queryController.route(sqlRouter);
|
|
|
|
this.copyController.route(sqlRouter);
|
|
|
|
this.jobController.route(sqlRouter);
|
|
|
|
|
|
|
|
paths.forEach(path => apiRouter.use(path, sqlRouter));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|