2nd
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
const yargs = require('yargs')
|
||||
const ws = require('ws')
|
||||
const fs = require('fs')
|
||||
const RedisConnexion = require('./redisConnexion')
|
||||
const urlparser = require('url');
|
||||
const wssServer = require('./wssServer')
|
||||
const configHelper = require('./configHelper')
|
||||
|
||||
|
||||
const argv = yargs.command('wssGateway', 'Redis <=> Websocket message bus gateway', {})
|
||||
.options({
|
||||
'argv.debug': {
|
||||
description: 'shows debug info',
|
||||
alias: 'd',
|
||||
type: 'boolean'
|
||||
},
|
||||
}).help().version('1.1').argv
|
||||
|
||||
const debug = Boolean(process.env.DEBUG) || argv.debug
|
||||
|
||||
const cfgh = new configHelper({
|
||||
localfile: './wssGatewayConfig.json',
|
||||
onAws: typeof(process.env.AWS_EXECUTION_ENV)=='string',
|
||||
awsRegion: 'eu-west-1',
|
||||
awsTable: 'bus-config',
|
||||
awsServiceName: 'wssGateway',
|
||||
})
|
||||
|
||||
async function startRedis(wssGatewayConfig) {
|
||||
let REDIScnx = new RedisConnexion({
|
||||
debug: debug,
|
||||
config: wssGatewayConfig,
|
||||
});
|
||||
if(debug) console.log('Starting REDIS...');
|
||||
await REDIScnx.redisLogin();
|
||||
if(debug) console.log('REDIS Login OK');
|
||||
await REDIScnx.redisChansStart();
|
||||
if(debug) console.log('REDIS ChansStart OK');
|
||||
return (REDIScnx);
|
||||
}
|
||||
|
||||
|
||||
cfgh.fetchConfig().then( wssGatewayConfig => {
|
||||
|
||||
if((!wssGatewayConfig) || (Object.keys(wssGatewayConfig).length<4)) {
|
||||
console.error('Cannot get a valid configuration ! Aaarrghhh...')
|
||||
process.exit()
|
||||
}
|
||||
let httpLib
|
||||
if(wssGatewayConfig.server.unsecure) httpLib = require('http')
|
||||
else httpLib = require('https')
|
||||
|
||||
|
||||
/////////////////////// Create & Start servers \\\\\\\\\\\\\\\\\\\\\\
|
||||
console.log(`Debug mode : ${debug ? 'ON' : 'OFF'}`)
|
||||
let options
|
||||
if(!wssGatewayConfig.server.unsecure) {
|
||||
options = {
|
||||
key: fs.readFileSync(wssGatewayConfig.server.certKeyFile),
|
||||
cert: fs.readFileSync(wssGatewayConfig.server.certFile),
|
||||
};
|
||||
} else options = {}
|
||||
|
||||
const httpRequestsHandler = function(request, res) {
|
||||
let parsedUrl
|
||||
try{
|
||||
parsedUrl = new urlparser.URL(request.url, `http${wssGatewayConfig.server.unsecure ? '': 's'}://${request.headers.host}`);
|
||||
} catch(e) {
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
if(parsedUrl.pathname === wssGatewayConfig.server.healthCheckPath) {
|
||||
//if(debug) console.log('Got a Health-Check Request')
|
||||
res.end(JSON.stringify({
|
||||
status: 'Healthy!',
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
const HTTPserver = httpLib.createServer(options, httpRequestsHandler)
|
||||
.listen(Number(wssGatewayConfig.server.listenPort),
|
||||
wssGatewayConfig.server.listenHost ? wssGatewayConfig.server.listenHost : undefined,
|
||||
function (req, res) {
|
||||
console.log(`HTTP${wssGatewayConfig.server.unsecure ? '': 'S'} now listening on ${wssGatewayConfig.server.listenHost}:${wssGatewayConfig.server.listenPort}\n`+
|
||||
`Websocket served at ${wssGatewayConfig.server.listenPath}\n`+
|
||||
`Healthcheck served at ${wssGatewayConfig.server.healthCheckPath}`)
|
||||
});
|
||||
|
||||
// Start serving WSS
|
||||
const wssServerOptions = {
|
||||
server: HTTPserver,
|
||||
path: wssGatewayConfig.server.listenPath,
|
||||
}
|
||||
if(!wssGatewayConfig.server.unsecure) {
|
||||
wssServerOptions['key'] = fs.readFileSync(wssGatewayConfig.server.certKeyFile)
|
||||
wssServerOptions['cert'] = fs.readFileSync(wssGatewayConfig.server.certFile)
|
||||
}
|
||||
const WSSServer = new ws.WebSocketServer(wssServerOptions);
|
||||
console.log(`WS${wssGatewayConfig.server.unsecure ? '': 'S'} server created for ${wssGatewayConfig.server.listenHost}:${wssGatewayConfig.server.listenPort}`)
|
||||
|
||||
startRedis(wssGatewayConfig).then((rediscnx) => {
|
||||
if(debug) console.log('Redis started & logged in !');
|
||||
const wssSrv = new wssServer(cfgh, WSSServer, rediscnx, debug);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user