2011-08-24 04:42:27 +08:00
|
|
|
// CartoDB SQL API
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
|
|
|
// all requests expect the following URL args:
|
|
|
|
// - `sql` {String} SQL to execute
|
|
|
|
//
|
|
|
|
// for private (read/write) queries:
|
2011-08-24 04:42:27 +08:00
|
|
|
// - OAuth. Must have proper OAuth 1.1 headers. For OAuth 1.1 spec see Google
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
2011-08-24 04:42:27 +08:00
|
|
|
// eg. /api/v1/?sql=SELECT 1 as one (with a load of OAuth headers or URL arguments)
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
|
|
|
// for public (read only) queries:
|
2011-08-24 04:42:27 +08:00
|
|
|
// - sql only, provided the subdomain exists in CartoDB and the table's sharing options are public
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
2011-08-24 04:42:27 +08:00
|
|
|
// eg. vizzuality.cartodb.com/api/v1/?sql=SELECT * from my_table
|
2012-07-24 16:29:47 +08:00
|
|
|
//
|
2015-12-04 00:28:18 +08:00
|
|
|
|
2015-05-13 18:22:41 +08:00
|
|
|
var express = require('express');
|
2016-10-04 20:40:04 +08:00
|
|
|
var Profiler = require('./stats/profiler-proxy');
|
2015-05-13 18:22:41 +08:00
|
|
|
var _ = require('underscore');
|
2017-06-27 21:12:23 +08:00
|
|
|
var TableCacheFactory = require('./utils/table_cache_factory');
|
2015-05-13 18:22:41 +08:00
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
var RedisPool = require('redis-mpool');
|
2016-10-17 21:02:34 +08:00
|
|
|
var cartodbRedis = require('cartodb-redis');
|
2016-01-22 00:24:48 +08:00
|
|
|
var UserDatabaseService = require('./services/user_database_service');
|
2018-03-01 18:31:35 +08:00
|
|
|
var UserLimitsService = require('./services/user_limits');
|
2016-10-12 00:28:46 +08:00
|
|
|
var JobPublisher = require('../batch/pubsub/job-publisher');
|
2016-01-22 00:24:48 +08:00
|
|
|
var JobQueue = require('../batch/job_queue');
|
|
|
|
var JobBackend = require('../batch/job_backend');
|
|
|
|
var JobCanceller = require('../batch/job_canceller');
|
2016-05-16 07:22:47 +08:00
|
|
|
var JobService = require('../batch/job_service');
|
|
|
|
|
2016-01-22 00:24:48 +08:00
|
|
|
var UserDatabaseMetadataService = require('../batch/user_database_metadata_service');
|
|
|
|
|
|
|
|
var cors = require('./middlewares/cors');
|
|
|
|
|
2015-12-04 01:35:49 +08:00
|
|
|
var GenericController = require('./controllers/generic_controller');
|
|
|
|
var QueryController = require('./controllers/query_controller');
|
2018-04-13 03:25:28 +08:00
|
|
|
var CopyController = require('./controllers/copy_controller');
|
2015-12-07 16:40:51 +08:00
|
|
|
var JobController = require('./controllers/job_controller');
|
2015-12-04 01:35:49 +08:00
|
|
|
var CacheStatusController = require('./controllers/cache_status_controller');
|
|
|
|
var HealthCheckController = require('./controllers/health_check_controller');
|
|
|
|
var VersionController = require('./controllers/version_controller');
|
|
|
|
|
2016-01-25 23:28:19 +08:00
|
|
|
var batchFactory = require('../batch');
|
2015-12-10 22:08:31 +08:00
|
|
|
|
2015-05-13 18:22:41 +08:00
|
|
|
process.env.PGAPPNAME = process.env.PGAPPNAME || 'cartodb_sqlapi';
|
|
|
|
|
2015-12-04 01:25:35 +08:00
|
|
|
// override Date.toJSON
|
2015-12-04 01:33:17 +08:00
|
|
|
require('./utils/date_to_json');
|
2012-11-12 19:37:34 +08:00
|
|
|
|
2017-03-30 22:13:17 +08:00
|
|
|
// jshint maxcomplexity:9
|
|
|
|
function App(statsClient) {
|
2014-01-31 17:55:30 +08:00
|
|
|
|
2016-09-27 00:09:27 +08:00
|
|
|
var app = express();
|
2012-11-12 19:37:34 +08:00
|
|
|
|
2016-10-17 21:02:34 +08:00
|
|
|
var redisPool = new RedisPool({
|
|
|
|
name: 'sql-api',
|
2015-12-04 00:28:18 +08:00
|
|
|
host: global.settings.redis_host,
|
|
|
|
port: global.settings.redis_port,
|
|
|
|
max: global.settings.redisPool,
|
|
|
|
idleTimeoutMillis: global.settings.redisIdleTimeoutMillis,
|
|
|
|
reapIntervalMillis: global.settings.redisReapIntervalMillis
|
2016-10-17 21:02:34 +08:00
|
|
|
});
|
|
|
|
var metadataBackend = cartodbRedis({ pool: redisPool });
|
2011-08-24 04:42:27 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
// Set default configuration
|
|
|
|
global.settings.db_pubuser = global.settings.db_pubuser || "publicuser";
|
|
|
|
global.settings.bufferedRows = global.settings.bufferedRows || 1000;
|
2018-05-11 21:41:35 +08:00
|
|
|
global.settings.ratelimits = Object.assign(
|
|
|
|
{
|
|
|
|
rateLimitsEnabled: false,
|
|
|
|
endpoints: {
|
|
|
|
query: false,
|
|
|
|
query_format: false,
|
|
|
|
job_create: false,
|
|
|
|
job_get: false,
|
|
|
|
job_delete: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
global.settings.ratelimits
|
|
|
|
);
|
2015-09-04 18:29:20 +08:00
|
|
|
|
2017-06-27 22:05:05 +08:00
|
|
|
var tableCache = new TableCacheFactory().build(global.settings);
|
2014-03-13 18:52:40 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
// Size based on https://github.com/CartoDB/cartodb.js/blob/3.15.2/src/geo/layer_definition.js#L72
|
|
|
|
var SQL_QUERY_BODY_LOG_MAX_LENGTH = 2000;
|
|
|
|
app.getSqlQueryFromRequestBody = function(req) {
|
|
|
|
var sqlQuery = req.body && req.body.q;
|
|
|
|
if (!sqlQuery) {
|
|
|
|
return '';
|
|
|
|
}
|
2015-09-04 18:29:20 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
if (sqlQuery.length > SQL_QUERY_BODY_LOG_MAX_LENGTH) {
|
|
|
|
sqlQuery = sqlQuery.substring(0, SQL_QUERY_BODY_LOG_MAX_LENGTH) + ' [...]';
|
2015-09-04 18:29:20 +08:00
|
|
|
}
|
2015-12-04 00:28:18 +08:00
|
|
|
return JSON.stringify({q: sqlQuery});
|
2015-09-04 18:29:20 +08:00
|
|
|
};
|
2014-03-20 18:22:13 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
if ( global.log4js ) {
|
|
|
|
var loggerOpts = {
|
|
|
|
buffer: true,
|
|
|
|
// log4js provides a tokens solution as expess but in does not provide the request/response in the callback.
|
|
|
|
// Thus it is not possible to extract relevant information from them.
|
|
|
|
// This is a workaround to be able to access request/response.
|
|
|
|
format: function(req, res, format) {
|
|
|
|
var logFormat = global.settings.log_format ||
|
|
|
|
':remote-addr :method :req[Host]:url :status :response-time ms -> :res[Content-Type]';
|
|
|
|
|
|
|
|
logFormat = logFormat.replace(/:sql/, app.getSqlQueryFromRequestBody(req));
|
|
|
|
return format(logFormat);
|
2014-03-20 18:22:13 +08:00
|
|
|
}
|
2015-12-04 00:28:18 +08:00
|
|
|
};
|
|
|
|
app.use(global.log4js.connectLogger(global.log4js.getLogger(), _.defaults(loggerOpts, {level:'info'})));
|
2015-05-13 18:22:41 +08:00
|
|
|
}
|
2014-03-20 01:34:21 +08:00
|
|
|
|
2016-01-22 00:24:48 +08:00
|
|
|
app.use(cors());
|
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
// Use step-profiler
|
2016-10-04 21:12:46 +08:00
|
|
|
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
|
2017-11-24 22:49:25 +08:00
|
|
|
res.locals = res.locals || {};
|
2016-10-04 21:12:46 +08:00
|
|
|
|
2016-10-04 21:43:19 +08:00
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:40:04 +08:00
|
|
|
var profile = global.settings.useProfiler;
|
|
|
|
req.profiler = new Profiler({
|
|
|
|
profile: profile,
|
2017-03-30 22:13:17 +08:00
|
|
|
statsd_client: statsClient
|
2016-10-04 20:40:04 +08:00
|
|
|
});
|
2015-12-04 00:28:18 +08:00
|
|
|
next();
|
2016-10-04 20:40:04 +08:00
|
|
|
});
|
2012-04-21 02:34:18 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
// Set connection timeout
|
|
|
|
if ( global.settings.hasOwnProperty('node_socket_timeout') ) {
|
|
|
|
var timeout = parseInt(global.settings.node_socket_timeout);
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
req.connection.setTimeout(timeout);
|
|
|
|
next();
|
|
|
|
});
|
2011-08-25 03:47:10 +08:00
|
|
|
}
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
app.enable('jsonp callback');
|
|
|
|
app.set("trust proxy", true);
|
2016-09-27 00:09:27 +08:00
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.disable('etag');
|
2012-04-13 07:30:45 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
// basic routing
|
2013-02-13 20:32:34 +08:00
|
|
|
|
2016-01-22 00:24:48 +08:00
|
|
|
var userDatabaseService = new UserDatabaseService(metadataBackend);
|
2018-03-01 18:31:35 +08:00
|
|
|
|
|
|
|
const userLimitsServiceOptions = {
|
|
|
|
limits: {
|
|
|
|
rateLimitsEnabled: global.settings.ratelimits.rateLimitsEnabled
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const userLimitsService = new UserLimitsService(metadataBackend, userLimitsServiceOptions);
|
2016-01-22 00:24:48 +08:00
|
|
|
|
2016-10-17 21:02:34 +08:00
|
|
|
var jobPublisher = new JobPublisher(redisPool);
|
2016-06-30 00:29:53 +08:00
|
|
|
var jobQueue = new JobQueue(metadataBackend, jobPublisher);
|
2016-08-31 01:04:21 +08:00
|
|
|
var jobBackend = new JobBackend(metadataBackend, jobQueue);
|
2016-01-22 00:24:48 +08:00
|
|
|
var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
|
2016-05-16 07:22:47 +08:00
|
|
|
var jobCanceller = new JobCanceller(userDatabaseMetadataService);
|
|
|
|
var jobService = new JobService(jobBackend, jobCanceller);
|
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
var genericController = new GenericController();
|
2015-12-04 01:43:13 +08:00
|
|
|
genericController.route(app);
|
2014-08-04 21:56:43 +08:00
|
|
|
|
2018-03-01 21:47:34 +08:00
|
|
|
var queryController = new QueryController(
|
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
|
|
|
tableCache,
|
|
|
|
statsClient,
|
|
|
|
userLimitsService
|
|
|
|
);
|
2015-12-04 01:43:13 +08:00
|
|
|
queryController.route(app);
|
2014-09-02 21:00:04 +08:00
|
|
|
|
2018-04-13 03:25:28 +08:00
|
|
|
var copyController = new CopyController(
|
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
2018-05-23 23:25:46 +08:00
|
|
|
userLimitsService
|
2018-04-13 03:25:28 +08:00
|
|
|
);
|
|
|
|
copyController.route(app);
|
|
|
|
|
2018-03-01 21:47:34 +08:00
|
|
|
var jobController = new JobController(
|
|
|
|
metadataBackend,
|
|
|
|
userDatabaseService,
|
|
|
|
jobService,
|
|
|
|
statsClient,
|
|
|
|
userLimitsService
|
|
|
|
);
|
2015-12-07 16:40:51 +08:00
|
|
|
jobController.route(app);
|
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
var cacheStatusController = new CacheStatusController(tableCache);
|
2015-12-04 01:43:13 +08:00
|
|
|
cacheStatusController.route(app);
|
2014-08-04 21:56:43 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
var healthCheckController = new HealthCheckController();
|
2015-12-04 01:43:13 +08:00
|
|
|
healthCheckController.route(app);
|
2014-08-04 21:56:43 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
var versionController = new VersionController();
|
2015-12-04 01:43:13 +08:00
|
|
|
versionController.route(app);
|
2014-01-31 17:55:30 +08:00
|
|
|
|
2016-01-09 01:29:36 +08:00
|
|
|
var isBatchProcess = process.argv.indexOf('--no-batch') === -1;
|
|
|
|
|
|
|
|
if (global.settings.environment !== 'test' && isBatchProcess) {
|
2016-10-11 01:46:07 +08:00
|
|
|
var batchName = global.settings.api_hostname || 'batch';
|
|
|
|
app.batch = batchFactory(
|
2017-03-30 22:13:17 +08:00
|
|
|
metadataBackend, redisPool, batchName, statsClient, global.settings.batch_log_filename
|
2016-10-11 01:46:07 +08:00
|
|
|
);
|
2016-01-25 23:28:19 +08:00
|
|
|
app.batch.start();
|
2015-12-11 01:40:44 +08:00
|
|
|
}
|
2015-12-10 22:08:31 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
return app;
|
2014-01-31 17:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = App;
|