106 lines
3.8 KiB
JavaScript
106 lines
3.8 KiB
JavaScript
import yargs from 'yargs/yargs'
|
|
import { hideBin } from 'yargs/helpers'
|
|
|
|
import { WebSocketServer } from 'ws'
|
|
import fs from 'fs'
|
|
import {RedisConnexion} from './redisConnexion.js'
|
|
import urlparser from 'url'
|
|
import {wssServer} from './wssServer.js'
|
|
import {configHelper} from './configHelper.js'
|
|
|
|
const argv = yargs(hideBin(process.argv)).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',
|
|
})
|
|
|
|
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( async 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 = await import('http')
|
|
else httpLib = await import('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 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);
|
|
});
|
|
|
|
|
|
})
|
|
|