2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-01 21:40:12 +08:00
|
|
|
const express = require('express');
|
|
|
|
const fs = require('fs');
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const RedisPool = require('redis-mpool');
|
|
|
|
const cartodbRedis = require('cartodb-redis');
|
2018-06-19 00:48:11 +08:00
|
|
|
const Logger = require('./services/logger');
|
2019-10-01 18:25:22 +08:00
|
|
|
const ApiRouter = require('./controllers/api-router');
|
2019-10-01 21:40:12 +08:00
|
|
|
const HealthCheckController = require('./controllers/health_check_controller');
|
|
|
|
const VersionController = require('./controllers/version_controller');
|
|
|
|
const 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
|
2019-10-02 00:22:13 +08:00
|
|
|
module.exports = function createServer (statsClient) {
|
2019-10-01 21:40:12 +08:00
|
|
|
const app = express();
|
|
|
|
const redisPool = new RedisPool({
|
2016-10-17 21:02:34 +08:00
|
|
|
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
|
|
|
});
|
2019-10-01 21:40:12 +08:00
|
|
|
const 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,
|
|
|
|
job_create: false,
|
|
|
|
job_get: false,
|
2019-02-27 20:14:16 +08:00
|
|
|
job_delete: false,
|
|
|
|
copy_from: false,
|
|
|
|
copy_to: false
|
2018-05-11 21:41:35 +08:00
|
|
|
}
|
2018-08-30 18:27:34 +08:00
|
|
|
},
|
2018-05-11 21:41:35 +08:00
|
|
|
global.settings.ratelimits
|
|
|
|
);
|
2015-09-04 18:29:20 +08:00
|
|
|
|
2019-10-01 19:15:13 +08:00
|
|
|
// TODO: it's here becouse of testing purposes, try to move to top level
|
2018-08-30 18:27:34 +08:00
|
|
|
global.settings.tmpDir = global.settings.tmpDir || '/tmp';
|
|
|
|
if (!fs.existsSync(global.settings.tmpDir)) {
|
|
|
|
mkdirp.sync(global.settings.tmpDir);
|
|
|
|
}
|
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
|
|
|
|
2018-06-19 00:48:11 +08:00
|
|
|
const dataIngestionLogger = new Logger(global.settings.dataIngestionLogPath, 'data-ingestion');
|
|
|
|
app.dataIngestionLogger = dataIngestionLogger;
|
|
|
|
|
2019-10-01 23:27:46 +08:00
|
|
|
// FIXME: health controller should be atached to the main entry point: "/"
|
2019-10-01 23:45:12 +08:00
|
|
|
// instead of "/api/v1/" or "/user/:user/api/:version"
|
2019-10-01 23:27:46 +08:00
|
|
|
const healthCheckController = new HealthCheckController({
|
2019-10-01 23:45:12 +08:00
|
|
|
routes: global.settings.routes.api
|
2019-10-01 23:27:46 +08:00
|
|
|
});
|
2015-12-04 01:43:13 +08:00
|
|
|
healthCheckController.route(app);
|
2014-08-04 21:56:43 +08:00
|
|
|
|
2019-10-01 23:27:46 +08:00
|
|
|
// FIXME: version controller should be atached to the main entry point: "/"
|
2019-10-01 23:45:12 +08:00
|
|
|
// instead of "/api/:version" or "/user/:user/api/:version"
|
2019-10-01 23:27:46 +08:00
|
|
|
const versionController = new VersionController({
|
2019-10-01 23:45:12 +08:00
|
|
|
routes: global.settings.routes.api
|
2019-10-01 23:27:46 +08:00
|
|
|
});
|
2015-12-04 01:43:13 +08:00
|
|
|
versionController.route(app);
|
2014-01-31 17:55:30 +08:00
|
|
|
|
2019-10-01 23:27:46 +08:00
|
|
|
const apiRouter = new ApiRouter({
|
2019-10-01 23:45:12 +08:00
|
|
|
routes: global.settings.routes.api,
|
2019-10-01 23:27:46 +08:00
|
|
|
redisPool,
|
|
|
|
metadataBackend,
|
|
|
|
statsClient,
|
|
|
|
dataIngestionLogger
|
|
|
|
});
|
2019-10-01 18:25:22 +08:00
|
|
|
apiRouter.route(app);
|
|
|
|
|
2019-10-01 21:40:12 +08:00
|
|
|
const isBatchProcess = process.argv.indexOf('--no-batch') === -1;
|
2016-01-09 01:29:36 +08:00
|
|
|
|
|
|
|
if (global.settings.environment !== 'test' && isBatchProcess) {
|
2019-10-01 21:40:12 +08:00
|
|
|
const batchName = global.settings.api_hostname || 'batch';
|
2019-10-01 18:25:22 +08:00
|
|
|
|
2016-10-11 01:46:07 +08:00
|
|
|
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
|
|
|
);
|
2019-10-01 18:25:22 +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;
|
2019-10-01 22:10:47 +08:00
|
|
|
};
|