CartoDB-SQL-API/app/server.js

101 lines
3.3 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-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',
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;
// 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"
const healthCheckController = new HealthCheckController({
2019-10-01 23:45:12 +08:00
routes: global.settings.routes.api
});
2015-12-04 01:43:13 +08:00
healthCheckController.route(app);
// 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"
const versionController = new VersionController({
2019-10-01 23:45:12 +08:00
routes: global.settings.routes.api
});
2015-12-04 01:43:13 +08:00
versionController.route(app);
const apiRouter = new ApiRouter({
2019-10-01 23:45:12 +08:00
routes: global.settings.routes.api,
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;
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
};