nsolid-manager
Version:
A manager that sets up and runs N|Solid for you
73 lines (59 loc) • 1.89 kB
JavaScript
process.title = 'nsolid-proxy'
var Listener = require('nsolid-listener')
var Routing = require('./router')
var debug = require('util').debuglog('nsolid')
var conf = require('rc')('nsolid-proxy')
if (conf.h || conf.help) {
const helpFile = require('path').join(__dirname, 'help.txt')
const helpText = require('fs').readFileSync(helpFile)
console.error(String(helpText))
process.exit(0)
}
if (conf.port == null) {
usageError('Could not find configuration for port')
}
conf.port = String(conf.port)
const portPattern = /^(.*:)?\d+/
if (!conf.port.match(portPattern)) {
usageError('Invalid port option value: ' + conf.port)
}
var listener = new Listener(conf.port)
listener.on('listening', function startRouter () {
var registryUri = process.env.REGISTRY || conf.registry || ''
debug('Connection to registry at: ', registryUri)
var options = {
etcd_host: registryUri,
denied: conf.denied,
broadcast_approved: conf.broadcast_approved
}
var router = new Routing(options)
// Hub commands
listener.on('ls', router.list.bind(router))
// Everything else -- proxy
listener.on('*', router.proxy.bind(router))
})
function gracefulShutdown (err) {
if (err instanceof Error) {
console.error('Graceful shutdown triggered due to error')
console.error(err)
console.error(err.stack)
}
console.log('Shutting down hub')
listener.server.close()
var wait = setTimeout(function () {
console.error('Forcing shutdown after wait')
process.exit(1)
}, 5000)
wait.unref()
}
process.on('uncaughtException', gracefulShutdown)
process.on('SIGINT', gracefulShutdown)
process.on('SIGTERM', gracefulShutdown)
process.on('SIGHUP', gracefulShutdown)
function usageError (message) {
console.error(message)
console.error('Use the "-h" option to display help on this command.')
process.exit(1)
}