CartoDB-SQL-API/app/controllers/copy_controller.js

150 lines
5.1 KiB
JavaScript
Raw Normal View History

'use strict';
const userMiddleware = require('../middlewares/user');
const errorMiddleware = require('../middlewares/error');
const authorizationMiddleware = require('../middlewares/authorization');
const connectionParamsMiddleware = require('../middlewares/connection-params');
const timeoutLimitsMiddleware = require('../middlewares/timeout-limits');
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
const rateLimitsMiddleware = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
2018-05-25 01:48:24 +08:00
const errorHandlerFactory = require('../services/error_handler_factory');
2018-05-26 00:47:41 +08:00
const streamCopy = require('../services/stream_copy');
2018-06-19 16:04:17 +08:00
function CopyController(metadataBackend, userDatabaseService, userLimitsService, logger) {
this.metadataBackend = metadataBackend;
this.userDatabaseService = userDatabaseService;
this.userLimitsService = userLimitsService;
this.logger = logger;
}
CopyController.prototype.route = function (app) {
const { base_url } = global.settings;
2018-05-08 18:52:33 +08:00
const copyFromMiddlewares = endpointGroup => {
return [
initializeProfilerMiddleware('copyfrom'),
userMiddleware(this.metadataBackend),
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService),
timeoutLimitsMiddleware(this.metadataBackend),
validateCopyQuery(),
2018-05-25 23:42:30 +08:00
handleCopyFrom(this.logger),
2018-05-25 23:50:59 +08:00
errorHandler(),
errorMiddleware()
];
};
2018-05-08 18:52:33 +08:00
const copyToMiddlewares = endpointGroup => {
return [
initializeProfilerMiddleware('copyto'),
userMiddleware(this.metadataBackend),
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService),
timeoutLimitsMiddleware(this.metadataBackend),
validateCopyQuery(),
2018-05-23 23:25:46 +08:00
handleCopyTo(this.logger),
2018-05-25 23:50:59 +08:00
errorHandler(),
errorMiddleware()
];
};
2018-05-04 00:50:13 +08:00
app.post(`${base_url}/sql/copyfrom`, copyFromMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_FROM));
app.get(`${base_url}/sql/copyto`, copyToMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_TO));
};
2018-05-23 23:25:46 +08:00
function handleCopyTo (logger) {
2018-05-22 17:54:10 +08:00
return function handleCopyToMiddleware (req, res, next) {
const filename = req.query.filename || 'carto-sql-copyto.dmp';
2018-05-08 18:52:33 +08:00
2018-05-22 17:54:10 +08:00
res.header("Content-Disposition", `attachment; filename=${encodeURIComponent(filename)}`);
res.header("Content-Type", "application/octet-stream");
2018-05-08 19:08:29 +08:00
2018-05-26 00:47:41 +08:00
streamCopy.to(
2018-05-25 23:42:30 +08:00
res,
req.query.q,
2018-06-05 00:08:34 +08:00
res.locals.userDbParams,
res.locals.user,
2018-05-26 00:47:41 +08:00
logger,
2018-05-26 00:50:56 +08:00
function(err) {
2018-05-25 23:50:59 +08:00
if (err) {
2018-05-25 23:42:30 +08:00
return next(err);
}
2018-05-25 23:50:59 +08:00
// this is a especial endpoint
// the data from postgres is streamed to response directly
2018-05-25 23:42:30 +08:00
}
2018-05-25 23:50:59 +08:00
);
2018-05-22 17:54:10 +08:00
};
}
2018-05-08 18:52:33 +08:00
2018-05-25 23:50:59 +08:00
function handleCopyFrom (logger) {
return function handleCopyFromMiddleware (req, res, next) {
2018-05-26 00:47:41 +08:00
streamCopy.from(
2018-05-25 23:42:30 +08:00
req,
req.query.q,
res.locals.userDbParams,
2018-06-05 00:08:34 +08:00
res.locals.user,
2018-05-25 23:42:30 +08:00
req.get('content-encoding') === 'gzip',
2018-05-26 00:47:41 +08:00
logger,
function(err, metrics) {
2018-05-25 23:42:30 +08:00
if (err) {
2018-05-25 23:50:59 +08:00
return next(err);
}
// TODO: change when data-ingestion log works: const { time, rows } = metrics;
const { time, rows, type, format, isGzip, size } = metrics;
2018-06-14 16:45:23 +08:00
if (!rows) {
2018-05-25 23:57:40 +08:00
return next(new Error("No rows copied"));
}
// TODO: remove when data-ingestion log works
if (req.profiler) {
req.profiler.add({copyFrom: { type, format, gzip: isGzip, size, rows, time }});
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
2018-05-25 23:57:40 +08:00
}
2018-05-25 23:50:59 +08:00
2018-05-26 00:47:41 +08:00
res.send({
time,
total_rows: rows
});
2018-05-25 21:46:12 +08:00
}
2018-05-26 00:50:56 +08:00
);
};
}
2018-05-08 18:52:33 +08:00
function validateCopyQuery () {
return function validateCopyQueryMiddleware (req, res, next) {
const sql = req.query.q;
if (!sql) {
2018-06-14 16:49:36 +08:00
return next(new Error("SQL is missing"));
}
// Only accept SQL that starts with 'COPY'
if (!sql.toUpperCase().startsWith("COPY ")) {
2018-06-14 16:49:36 +08:00
return next(new Error("SQL must start with COPY"));
}
2018-05-22 22:02:14 +08:00
next();
};
}
2018-05-25 23:50:59 +08:00
function errorHandler () {
return function errorHandlerMiddleware (err, req, res, next) {
if (res.headersSent) {
console.error("EXCEPTION REPORT: " + err.stack);
2018-05-25 23:50:59 +08:00
const errorHandler = errorHandlerFactory(err);
res.write(JSON.stringify(errorHandler.getResponse()));
res.end();
} else {
return next(err);
}
};
}
2018-05-04 00:31:49 +08:00
module.exports = CopyController;