CartoDB-SQL-API/lib/server.js

85 lines
2.6 KiB
JavaScript
Raw Normal View History

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');
const Logger = require('./services/logger');
const ApiRouter = require('./api/api-router');
const batchFactory = require('./batch');
const getServerOptions = require('./server-options');
process.env.PGAPPNAME = process.env.PGAPPNAME || 'cartodb_sqlapi';
2015-12-04 01:25:35 +08:00
// override Date.toJSON
require('./utils/date-to-json');
2019-10-02 00:22:13 +08:00
module.exports = function createServer (statsClient) {
const options = 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',
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 });
// Set default configuration
2019-12-24 01:19:08 +08:00
global.settings.db_pubuser = global.settings.db_pubuser || 'publicuser';
global.settings.bufferedRows = global.settings.bufferedRows || 1000;
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-08-30 18:27:34 +08:00
},
global.settings.ratelimits
);
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);
}
app.enable('jsonp callback');
2019-12-24 01:19:08 +08:00
app.set('trust proxy', true);
app.disable('x-powered-by');
app.disable('etag');
const dataIngestionLogger = new Logger(global.settings.dataIngestionLogPath, 'data-ingestion');
app.dataIngestionLogger = dataIngestionLogger;
const apiRouter = new ApiRouter({
redisPool,
metadataBackend,
statsClient,
dataIngestionLogger
});
apiRouter.route(app, options.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;
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
app.batch = batchFactory(
2017-03-30 22:13:17 +08:00
metadataBackend, redisPool, batchName, statsClient, global.settings.batch_log_filename
);
2019-10-01 18:25:22 +08:00
app.batch.start();
}
return app;
2019-10-01 22:10:47 +08:00
};