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

154 lines
5.2 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-28 17:18:30 +08:00
const BunyanLogger = require('../services/bunyan_logger');
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-05-22 17:54:10 +08:00
function CopyController(metadataBackend, userDatabaseService, userLimitsService, statsClient) {
this.metadataBackend = metadataBackend;
this.userDatabaseService = userDatabaseService;
this.userLimitsService = userLimitsService;
2018-05-22 17:54:10 +08:00
this.statsClient = statsClient;
2018-05-23 23:25:46 +08:00
2018-05-28 17:25:09 +08:00
this.logger = new BunyanLogger(
global.settings.dataIngestionLogPath || 'log/data-ingestion.log',
'data-ingestion'
);
}
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(),
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(),
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,
res.locals.userDbParams,
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,
req.get('content-encoding') === 'gzip',
2018-05-26 00:47:41 +08:00
logger,
2018-05-26 00:50:56 +08:00
function(err, metrics) { // TODO: remove when data-ingestion log works: {time, rows}
2018-05-25 23:42:30 +08:00
if (err) {
2018-05-25 23:50:59 +08:00
return next(err);
}
2018-05-26 00:47:41 +08:00
// TODO: remove when data-ingestion log works
2018-05-26 00:50:56 +08:00
const { time, rows, type, format, gzip, size } = metrics;
2018-05-26 00:47:41 +08:00
if (!time || !rows) {
2018-05-25 23:57:40 +08:00
return next(new Error("No rows copied"));
}
2018-05-25 23:50:59 +08:00
// TODO: remove when data-ingestion log works
if (req.profiler) {
2018-05-26 00:47:41 +08:00
req.profiler.add({copyFrom: { type, format, gzip, size, rows, time }});
2018-05-25 23:50:59 +08:00
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
2018-05-25 23:42:30 +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) {
next(new Error("SQL is missing"));
}
// Only accept SQL that starts with 'COPY'
if (!sql.toUpperCase().startsWith("COPY ")) {
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) {
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;