-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathserver.js
76 lines (64 loc) · 2.38 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const express = require('express');
const fs = require('fs');
const RedisPool = require('redis-mpool');
const cartodbRedis = require('cartodb-redis');
const ApiRouter = require('./api/api-router');
const batchFactory = require('./batch');
const getServerOptions = require('./server-options');
process.env.PGAPPNAME = process.env.PGAPPNAME || 'cartodb_sqlapi';
// override Date.toJSON
require('./utils/date-to-json');
module.exports = function createServer (statsClient) {
const { routes, logger } = getServerOptions();
const app = express();
const redisPool = new RedisPool({
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
});
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,
job_delete: false,
copy_from: false,
copy_to: false
}
},
global.settings.ratelimits
);
// TODO: it's here becouse of testing purposes, try to move to top level
global.settings.tmpDir = global.settings.tmpDir || '/tmp';
if (!fs.existsSync(global.settings.tmpDir)) {
fs.mkdirSync(global.settings.tmpDir, { recursive: true });
}
app.enable('jsonp callback');
app.set('trust proxy', true);
app.disable('x-powered-by');
app.disable('etag');
const apiRouter = new ApiRouter({
redisPool,
metadataBackend,
statsClient,
logger
});
apiRouter.route(app, routes.api);
const isBatchProcess = process.argv.indexOf('--no-batch') === -1;
if (global.settings.environment !== 'test' && isBatchProcess) {
const batchName = global.settings.api_hostname || 'batch';
app.batch = batchFactory(metadataBackend, redisPool, batchName, statsClient, logger);
app.batch.start();
}
return app;
};