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/AphlictListenerList', __dirname); | ||||
JX.require('lib/AphlictLog', __dirname); | JX.require('lib/AphlictLog', __dirname); | ||||
function parse_command_line_arguments(argv) { | function parse_command_line_arguments(argv) { | ||||
var config = { | var config = { | ||||
port: 22280, | 'client-port': 22280, | ||||
admin: 22281, | 'admin-port': 22281, | ||||
host: '127.0.0.1', | 'client-host': '0.0.0.0', | ||||
'admin-host': '127.0.0.1', | |||||
log: '/var/log/aphlict.log', | log: '/var/log/aphlict.log', | ||||
epriestley: This cleanup is nice. | |||||
'ssl-key': null, | 'ssl-key': null, | ||||
'ssl-cert': null, | 'ssl-cert': null, | ||||
test: false | test: false | ||||
}; | }; | ||||
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) { | ||||
throw new Error("Unknown argument '" + arg + "'!"); | throw new Error("Unknown argument '" + arg + "'!"); | ||||
} | } | ||||
if (!(matches[1] in config)) { | if (!(matches[1] in config)) { | ||||
throw new Error("Unknown argument '" + matches[1] + "'!"); | throw new Error("Unknown argument '" + matches[1] + "'!"); | ||||
} | } | ||||
config[matches[1]] = matches[2]; | config[matches[1]] = matches[2]; | ||||
} | } | ||||
config.port = parseInt(config.port, 10); | config['client-port'] = parseInt(config['client-port'], 10); | ||||
config.admin = parseInt(config.admin, 10); | config['admin-port'] = parseInt(config['admin-port'], 10); | ||||
return config; | return config; | ||||
} | } | ||||
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); | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | function https_discard_handler(req, res) { | ||||
res.end('HTTP/501 Use Websockets\n'); | res.end('HTTP/501 Use Websockets\n'); | ||||
} | } | ||||
var ws; | var ws; | ||||
if (ssl_config.enabled) { | if (ssl_config.enabled) { | ||||
var https_server = https.createServer({ | var https_server = https.createServer({ | ||||
key: ssl_config.key, | key: ssl_config.key, | ||||
cert: ssl_config.cert | cert: ssl_config.cert | ||||
}, https_discard_handler).listen(config.port); | }, https_discard_handler).listen( | ||||
config['client-port'], | |||||
config['client-host']); | |||||
ws = new WebSocket.Server({server: https_server}); | ws = new WebSocket.Server({server: https_server}); | ||||
} else { | } else { | ||||
ws = new WebSocket.Server({port: config.port}); | ws = new WebSocket.Server({ | ||||
port: config['client-port'], | |||||
host: config['client-host'], | |||||
}); | |||||
} | } | ||||
ws.on('connection', function(ws) { | ws.on('connection', function(ws) { | ||||
var listener = clients.addListener(ws); | var listener = clients.addListener(ws); | ||||
function log() { | function log() { | ||||
debug.log( | debug.log( | ||||
util.format('<%s>', listener.getDescription()) + | util.format('<%s>', listener.getDescription()) + | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | if (request.url == '/') { | ||||
response.writeHead(200, {'Content-Type': 'application/json'}); | response.writeHead(200, {'Content-Type': 'application/json'}); | ||||
response.write(JSON.stringify(status)); | response.write(JSON.stringify(status)); | ||||
response.end(); | response.end(); | ||||
} else { | } else { | ||||
response.writeHead(404, 'Not Found'); | response.writeHead(404, 'Not Found'); | ||||
response.end(); | response.end(); | ||||
} | } | ||||
}).listen(config.admin, config.host); | }).listen(config['admin-port'], config['admin-host']); | ||||
debug.log('Started Server (PID %d)', process.pid); | debug.log('Started Server (PID %d)', process.pid); |
This cleanup is nice.