Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/aphlict_server.js
| 'use strict'; | 'use strict'; | ||||
| var JX = require('./lib/javelin').JX; | var JX = require('./lib/javelin').JX; | ||||
| var http = require('http'); | var http = require('http'); | ||||
| var https = require('https'); | var https = require('https'); | ||||
| var util = require('util'); | var util = require('util'); | ||||
| var fs = require('fs'); | var fs = require('fs'); | ||||
| function parse_command_line_arguments(argv) { | function parse_command_line_arguments(argv) { | ||||
| var args = { | var args = { | ||||
| log: '/var/log/aphlict.log', | |||||
| test: false, | test: false, | ||||
| config: null | config: null | ||||
| }; | }; | ||||
| for (var ii = 2; ii < argv.length; ii++) { | for (var ii = 2; ii < argv.length; ii++) { | ||||
| var arg = argv[ii]; | var arg = argv[ii]; | ||||
| var matches = arg.match(/^--([^=]+)=(.*)$/); | var matches = arg.match(/^--([^=]+)=(.*)$/); | ||||
| if (!matches) { | if (!matches) { | ||||
| Show All 24 Lines | |||||
| function set_exit_code(code) { | function set_exit_code(code) { | ||||
| process.on('exit', function() { | process.on('exit', function() { | ||||
| process.exit(code); | process.exit(code); | ||||
| }); | }); | ||||
| } | } | ||||
| process.on('uncaughtException', function(err) { | process.on('uncaughtException', function(err) { | ||||
| var context = null; | var context = null; | ||||
| if (err.code == 'EACCES' && err.path == args.log) { | if (err.code == 'EACCES') { | ||||
| context = util.format( | context = util.format( | ||||
| 'Unable to open logfile ("%s"). Check that permissions are set ' + | 'Unable to open file ("%s"). Check that permissions are set ' + | ||||
| 'correctly.', | 'correctly.', | ||||
| err.path); | err.path); | ||||
| } | } | ||||
| var message = [ | var message = [ | ||||
| '\n<<< UNCAUGHT EXCEPTION! >>>', | '\n<<< UNCAUGHT EXCEPTION! >>>', | ||||
| ]; | ]; | ||||
| if (context) { | if (context) { | ||||
| message.push(context); | message.push(context); | ||||
| } | } | ||||
| message.push(err.stack); | message.push(err.stack); | ||||
| debug.log(message.join('\n\n')); | debug.log(message.join('\n\n')); | ||||
| set_exit_code(1); | set_exit_code(1); | ||||
| }); | }); | ||||
| // Add the logfile so we'll fail if we can't write to it. | |||||
| if (args.log) { | |||||
| debug.addLog(args.log); | |||||
| } | |||||
| try { | try { | ||||
| require('ws'); | require('ws'); | ||||
| } catch (ex) { | } catch (ex) { | ||||
| throw new Error( | throw new Error( | ||||
| 'You need to install the Node.js "ws" module for websocket support. ' + | 'You need to install the Node.js "ws" module for websocket support. ' + | ||||
| 'See "Notifications User Guide: Setup and Configuration" in the ' + | 'See "Notifications User Guide: Setup and Configuration" in the ' + | ||||
| 'documentation for instructions. ' + ex.toString()); | 'documentation for instructions. ' + ex.toString()); | ||||
| } | } | ||||
| // NOTE: Require these only after checking for the "ws" module, since they | // NOTE: Require these only after checking for the "ws" module, since they | ||||
| // depend on it. | // depend on it. | ||||
| require('./lib/AphlictAdminServer'); | require('./lib/AphlictAdminServer'); | ||||
| require('./lib/AphlictClientServer'); | require('./lib/AphlictClientServer'); | ||||
| var ii; | var ii; | ||||
| var logs = config.logs || []; | |||||
| for (ii = 0; ii < logs.length; ii++) { | |||||
| debug.addLog(logs[ii].path); | |||||
| } | |||||
| var servers = []; | var servers = []; | ||||
| for (ii = 0; ii < config.servers.length; ii++) { | for (ii = 0; ii < config.servers.length; ii++) { | ||||
| var spec = config.servers[ii]; | var spec = config.servers[ii]; | ||||
| spec.listen = spec.listen || '0.0.0.0'; | spec.listen = spec.listen || '0.0.0.0'; | ||||
| if (spec['ssl.key']) { | if (spec['ssl.key']) { | ||||
| spec['ssl.key'] = fs.readFileSync(spec['ssl.key']); | spec['ssl.key'] = fs.readFileSync(spec['ssl.key']); | ||||
| Show All 11 Lines | |||||
| if (args.test) { | if (args.test) { | ||||
| debug.log('Configuration test OK.'); | debug.log('Configuration test OK.'); | ||||
| set_exit_code(0); | set_exit_code(0); | ||||
| return; | return; | ||||
| } | } | ||||
| debug.log('Starting servers (service PID %d).', process.pid); | debug.log('Starting servers (service PID %d).', process.pid); | ||||
| for (ii = 0; ii < logs.length; ii++) { | |||||
| debug.log('Logging to "%s".', logs[ii].path); | |||||
| } | |||||
| var aphlict_servers = []; | var aphlict_servers = []; | ||||
| var aphlict_clients = []; | var aphlict_clients = []; | ||||
| var aphlict_admins = []; | var aphlict_admins = []; | ||||
| for (ii = 0; ii < servers.length; ii++) { | for (ii = 0; ii < servers.length; ii++) { | ||||
| var server = servers[ii]; | var server = servers[ii]; | ||||
| var is_client = (server.type == 'client'); | var is_client = (server.type == 'client'); | ||||
| var http_server; | var http_server; | ||||
| Show All 40 Lines | |||||