Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/aphlict_server.js
| 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'); | ||||
| JX.require('lib/AphlictListenerList', __dirname); | |||||
| JX.require('lib/AphlictLog', __dirname); | |||||
| function parse_command_line_arguments(argv) { | function parse_command_line_arguments(argv) { | ||||
| var config = { | var config = { | ||||
joshuaspence: I think that by `require`-ing this here, we bypass the whole `try { require('ws'); }` stuff. | |||||
| 'client-port': 22280, | 'client-port': 22280, | ||||
| 'admin-port': 22281, | 'admin-port': 22281, | ||||
| 'client-host': '0.0.0.0', | 'client-host': '0.0.0.0', | ||||
| 'admin-host': '127.0.0.1', | 'admin-host': '127.0.0.1', | ||||
| log: '/var/log/aphlict.log', | log: '/var/log/aphlict.log', | ||||
| 'ssl-key': null, | 'ssl-key': null, | ||||
| 'ssl-cert': null, | 'ssl-cert': null, | ||||
| test: false | test: false | ||||
| Show All 12 Lines | function parse_command_line_arguments(argv) { | ||||
| } | } | ||||
| config['client-port'] = parseInt(config['client-port'], 10); | config['client-port'] = parseInt(config['client-port'], 10); | ||||
| config['admin-port'] = parseInt(config['admin-port'], 10); | config['admin-port'] = parseInt(config['admin-port'], 10); | ||||
| return config; | return config; | ||||
| } | } | ||||
| require('./lib/AphlictLog'); | |||||
| var debug = new JX.AphlictLog() | var debug = new JX.AphlictLog() | ||||
| .addConsole(console); | .addConsole(console); | ||||
| var config = parse_command_line_arguments(process.argv); | var config = parse_command_line_arguments(process.argv); | ||||
| process.on('uncaughtException', function(err) { | process.on('uncaughtException', function(err) { | ||||
| var context = null; | var context = null; | ||||
| if (err.code == 'EACCES' && err.path == config.log) { | if (err.code == 'EACCES' && err.path == config.log) { | ||||
| Show All 18 Lines | 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 | |||||
| // depend on it. | |||||
| require('./lib/AphlictAdminServer'); | |||||
| require('./lib/AphlictClientServer'); | |||||
| var ssl_config = { | var ssl_config = { | ||||
| enabled: (config['ssl-key'] || config['ssl-cert']) | enabled: (config['ssl-key'] || config['ssl-cert']) | ||||
| }; | }; | ||||
| // Load the SSL certificates (if any were provided) now, so that runs with | // Load the SSL certificates (if any were provided) now, so that runs with | ||||
| // `--test` will see any errors. | // `--test` will see any errors. | ||||
| if (ssl_config.enabled) { | if (ssl_config.enabled) { | ||||
| ssl_config.key = fs.readFileSync(config['ssl-key']); | ssl_config.key = fs.readFileSync(config['ssl-key']); | ||||
| ssl_config.cert = fs.readFileSync(config['ssl-cert']); | ssl_config.cert = fs.readFileSync(config['ssl-cert']); | ||||
| } | } | ||||
| // Add the logfile so we'll fail if we can't write to it. | // Add the logfile so we'll fail if we can't write to it. | ||||
| if (config.log) { | if (config.log) { | ||||
| debug.addLog(config.log); | debug.addLog(config.log); | ||||
| } | } | ||||
| // If we're just doing a configuration test, exit here before starting any | // If we're just doing a configuration test, exit here before starting any | ||||
| // servers. | // servers. | ||||
| if (config.test) { | if (config.test) { | ||||
| debug.log('Configuration test OK.'); | debug.log('Configuration test OK.'); | ||||
| process.exit(0); | process.exit(0); | ||||
| } | } | ||||
| JX.require('lib/AphlictAdminServer', __dirname); | |||||
| JX.require('lib/AphlictClientServer', __dirname); | |||||
| var server; | var server; | ||||
| if (ssl_config.enabled) { | if (ssl_config.enabled) { | ||||
| server = https.createServer({ | server = https.createServer({ | ||||
| key: ssl_config.key, | key: ssl_config.key, | ||||
| cert: ssl_config.cert | cert: ssl_config.cert | ||||
| }, function(req, res) { | }, function(req, res) { | ||||
| res.writeHead(501); | res.writeHead(501); | ||||
| res.end('HTTP/501 Use Websockets\n'); | res.end('HTTP/501 Use Websockets\n'); | ||||
| Show All 16 Lines | |||||
I think that by require-ing this here, we bypass the whole try { require('ws'); } stuff. Specifically, I would expect this to throw an unhelpful error message if the ws module is missing.