2015-08-27 22:28:16 +08:00
|
|
|
var http = require('http');
|
|
|
|
var https = require('https');
|
2015-04-07 19:00:20 +08:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2015-04-07 19:00:20 +08:00
|
|
|
var _ = require('underscore');
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
var ENVIRONMENT;
|
2015-04-07 19:00:20 +08:00
|
|
|
if ( process.argv[2] ) {
|
2015-07-05 05:09:00 +08:00
|
|
|
ENVIRONMENT = process.argv[2];
|
2015-04-07 19:00:20 +08:00
|
|
|
} else if ( process.env.NODE_ENV ) {
|
2015-07-05 05:09:00 +08:00
|
|
|
ENVIRONMENT = process.env.NODE_ENV;
|
2015-04-07 19:00:20 +08:00
|
|
|
} else {
|
2015-07-05 05:09:00 +08:00
|
|
|
ENVIRONMENT = 'development';
|
2015-04-07 19:00:20 +08:00
|
|
|
}
|
2014-02-28 23:22:24 +08:00
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
var availableEnvironments = {
|
|
|
|
production: true,
|
|
|
|
staging: true,
|
|
|
|
development: true
|
|
|
|
};
|
2014-02-28 23:22:24 +08:00
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// sanity check
|
2015-07-05 05:09:00 +08:00
|
|
|
if (!availableEnvironments[ENVIRONMENT]){
|
|
|
|
console.error('node app.js [environment]');
|
|
|
|
console.error('environments: %s', Object.keys(availableEnvironments).join(', '));
|
2011-10-07 22:38:28 +08:00
|
|
|
process.exit(1);
|
2011-09-05 07:00:41 +08:00
|
|
|
}
|
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
process.env.NODE_ENV = ENVIRONMENT;
|
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// set environment specific variables
|
2015-07-05 05:09:00 +08:00
|
|
|
global.environment = require('./config/environments/' + ENVIRONMENT);
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2014-09-24 17:42:36 +08:00
|
|
|
global.log4js = require('log4js');
|
2015-04-07 19:00:20 +08:00
|
|
|
var log4js_config = {
|
2014-03-29 01:05:18 +08:00
|
|
|
appenders: [],
|
2015-07-05 05:09:00 +08:00
|
|
|
replaceConsole: true
|
2014-03-13 00:20:49 +08:00
|
|
|
};
|
|
|
|
|
2014-10-08 22:50:35 +08:00
|
|
|
if (global.environment.uv_threadpool_size) {
|
|
|
|
process.env.UV_THREADPOOL_SIZE = global.environment.uv_threadpool_size;
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:54:56 +08:00
|
|
|
// set global HTTP and HTTPS agent default configurations
|
|
|
|
// ref https://nodejs.org/api/http.html#http_new_agent_options
|
|
|
|
var agentOptions = _.defaults(global.environment.httpAgent || {}, {
|
|
|
|
keepAlive: false,
|
|
|
|
keepAliveMsecs: 1000,
|
|
|
|
maxSockets: Infinity,
|
|
|
|
maxFreeSockets: 256
|
|
|
|
});
|
|
|
|
http.globalAgent = new http.Agent(agentOptions);
|
|
|
|
https.globalAgent = new https.Agent(agentOptions);
|
|
|
|
|
2014-03-29 01:05:18 +08:00
|
|
|
if ( global.environment.log_filename ) {
|
2014-05-07 17:03:25 +08:00
|
|
|
var logdir = path.dirname(global.environment.log_filename);
|
|
|
|
// See cwd inlog4js.configure call below
|
|
|
|
logdir = path.resolve(__dirname, logdir);
|
|
|
|
if ( ! fs.existsSync(logdir) ) {
|
|
|
|
console.error("Log filename directory does not exist: " + logdir);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2014-03-29 01:05:18 +08:00
|
|
|
console.log("Logs will be written to " + global.environment.log_filename);
|
|
|
|
log4js_config.appenders.push(
|
|
|
|
{ type: "file", filename: global.environment.log_filename }
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
log4js_config.appenders.push(
|
|
|
|
{ type: "console", layout: { type:'basic' } }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:00:20 +08:00
|
|
|
global.log4js.configure(log4js_config, { cwd: __dirname });
|
|
|
|
global.logger = global.log4js.getLogger();
|
2014-02-18 22:12:08 +08:00
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
global.environment.api_hostname = require('os').hostname().split('.')[0];
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
// Include cartodb_windshaft only _after_ the "global" variable is set
|
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/28
|
2015-07-05 05:09:00 +08:00
|
|
|
var cartodbWindshaft = require('./lib/cartodb/server');
|
|
|
|
var serverOptions = require('./lib/cartodb/server_options');
|
2013-01-30 00:36:50 +08:00
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
var server = cartodbWindshaft(serverOptions);
|
2014-10-15 04:12:35 +08:00
|
|
|
|
2013-01-30 00:36:50 +08:00
|
|
|
// Maximum number of connections for one process
|
|
|
|
// 128 is a good number if you have up to 1024 filedescriptors
|
|
|
|
// 4 is good if you have max 32 filedescriptors
|
|
|
|
// 1 is good if you have max 16 filedescriptors
|
2015-07-05 05:09:00 +08:00
|
|
|
server.maxConnections = global.environment.maxConnections || 128;
|
2013-01-30 00:36:50 +08:00
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
server.listen(serverOptions.bind.port, serverOptions.bind.host);
|
2013-01-30 00:36:50 +08:00
|
|
|
|
2014-03-11 19:21:00 +08:00
|
|
|
var version = require("./package").version;
|
|
|
|
|
2015-07-05 05:09:00 +08:00
|
|
|
server.on('listening', function() {
|
2015-08-24 18:45:21 +08:00
|
|
|
console.log(
|
|
|
|
"Windshaft tileserver %s started on %s:%s PID=%d (%s)",
|
2015-08-28 19:54:56 +08:00
|
|
|
version, serverOptions.bind.host, serverOptions.bind.port, process.pid, ENVIRONMENT
|
2015-08-24 18:45:21 +08:00
|
|
|
);
|
2012-10-09 23:09:40 +08:00
|
|
|
});
|
2013-01-30 18:29:44 +08:00
|
|
|
|
2015-08-28 19:54:56 +08:00
|
|
|
setInterval(function() {
|
|
|
|
var memoryUsage = process.memoryUsage();
|
|
|
|
Object.keys(memoryUsage).forEach(function(k) {
|
|
|
|
global.statsClient.gauge('windshaft.memory.' + k, memoryUsage[k]);
|
|
|
|
});
|
|
|
|
}, 5000);
|
|
|
|
|
2014-03-29 01:05:18 +08:00
|
|
|
process.on('SIGHUP', function() {
|
2014-12-02 22:05:28 +08:00
|
|
|
global.log4js.clearAndShutdownAppenders(function() {
|
|
|
|
global.log4js.configure(log4js_config);
|
2015-04-07 19:00:20 +08:00
|
|
|
global.logger = global.log4js.getLogger();
|
2014-12-02 22:05:28 +08:00
|
|
|
console.log('Log files reloaded');
|
|
|
|
});
|
2013-03-18 23:37:31 +08:00
|
|
|
});
|
2014-02-18 22:12:08 +08:00
|
|
|
|
2014-03-13 00:20:49 +08:00
|
|
|
process.on('uncaughtException', function(err) {
|
2015-04-07 19:00:20 +08:00
|
|
|
global.logger.error('Uncaught exception: ' + err.stack);
|
2014-03-13 00:20:49 +08:00
|
|
|
});
|