CartoDB-SQL-API/app/server.js

87 lines
2.9 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');
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');
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');
2017-03-30 22:13:17 +08:00
// jshint maxcomplexity:9
2019-10-01 21:50:05 +08:00
module.exports = function serverFactory (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',
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
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');
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;
2019-10-01 21:40:12 +08:00
const healthCheckController = new HealthCheckController();
2015-12-04 01:43:13 +08:00
healthCheckController.route(app);
2019-10-01 21:40:12 +08:00
const versionController = new VersionController();
2015-12-04 01:43:13 +08:00
versionController.route(app);
2019-10-01 18:25:22 +08:00
const apiRouter = new ApiRouter({ redisPool, metadataBackend, statsClient, dataIngestionLogger });
apiRouter.route(app);
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
};