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 RedisPool = require('redis-mpool');
|
|
|
|
const cartodbRedis = require('cartodb-redis');
|
2019-10-04 00:24:39 +08:00
|
|
|
const ApiRouter = require('./api/api-router');
|
|
|
|
const batchFactory = require('./batch');
|
2019-10-02 23:35:34 +08:00
|
|
|
const getServerOptions = require('./server-options');
|
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
|
2019-10-04 00:24:39 +08:00
|
|
|
require('./utils/date-to-json');
|
2012-11-12 19:37:34 +08:00
|
|
|
|
2019-10-02 00:22:13 +08:00
|
|
|
module.exports = function createServer (statsClient) {
|
2020-06-30 23:42:59 +08:00
|
|
|
const { routes, logger } = getServerOptions();
|
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
|
2019-12-24 01:19:08 +08:00
|
|
|
global.settings.db_pubuser = global.settings.db_pubuser || 'publicuser';
|
2015-12-04 00:28:18 +08:00
|
|
|
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)) {
|
2020-06-01 16:55:18 +08:00
|
|
|
fs.mkdirSync(global.settings.tmpDir, { recursive: true });
|
2018-08-30 18:27:34 +08:00
|
|
|
}
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
app.enable('jsonp callback');
|
2019-12-24 01:19:08 +08:00
|
|
|
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
|
|
|
|
2019-10-01 23:27:46 +08:00
|
|
|
const apiRouter = new ApiRouter({
|
|
|
|
redisPool,
|
|
|
|
metadataBackend,
|
|
|
|
statsClient,
|
2020-06-30 23:42:59 +08:00
|
|
|
logger
|
2019-10-01 23:27:46 +08:00
|
|
|
});
|
2020-06-30 23:42:59 +08:00
|
|
|
apiRouter.route(app, routes.api);
|
2019-10-01 18:25:22 +08:00
|
|
|
|
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
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
app.batch = batchFactory(metadataBackend, redisPool, batchName, statsClient, logger);
|
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
|
|
|
};
|