Commit a21490d3 by Amir Aharon

adjust to docker

parent 030d176e
Showing with 29 additions and 12 deletions
...@@ -9,7 +9,23 @@ const request = require('request'); ...@@ -9,7 +9,23 @@ const request = require('request');
var myArgs = require('optimist').argv, var myArgs = require('optimist').argv,
help = 'node app.js -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]'; help = 'node app.js -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]';
var config = {};
//const twoDaysAgoMs = 1000 * 3600 * 47; //const twoDaysAgoMs = 1000 * 3600 * 47;
/**
* getting configuration either from command args or from env
*/
var getConfiguration = function(){
myArgs.f ?
config.file = myArgs.f : config.file = process.env.file;
myArgs.p ?
config.port = myArgs.p : config.port = 8053; // default port
myArgs.d ?
config.delay = myArgs.d : config.delay = process.env.delay;
myArgs.postUrl ?
config.postUrl = myArgs.postUrl : config.postUrl = process.env.postUrl;
console.log("Configuration is: " + JSON.stringify(config));
};
/** /**
* some validation * some validation
...@@ -18,18 +34,19 @@ if ((myArgs.h)||(myArgs.help)) { ...@@ -18,18 +34,19 @@ if ((myArgs.h)||(myArgs.help)) {
console.log(help); console.log(help);
process.exit(0); process.exit(0);
} }
if (myArgs.f == null){
getConfiguration();
if (config.file == null){
console.log('please insert file path'); console.log('please insert file path');
process.exit(0); process.exit(0);
} }
if (myArgs.d >0){ if (config.delay && config.delay > 0){
rule = new schedule.RecurrenceRule(); rule = new schedule.RecurrenceRule();
rule.second = myArgs.d; rule.second = config.delay;
rule.minute = 0; rule.minute = 0;
} }
console.log('myArgs: ', myArgs);
const app = express(); const app = express();
app.use(function (req, res) { app.use(function (req, res) {
...@@ -104,13 +121,13 @@ var sendJson = function(){ ...@@ -104,13 +121,13 @@ var sendJson = function(){
var newEventObj = JSON.parse(JSON.stringify(eventObj)) var newEventObj = JSON.parse(JSON.stringify(eventObj))
normalyzeEvent(newEventObj); normalyzeEvent(newEventObj);
console.log("sending: " + JSON.stringify(newEventObj)); console.log("sending: " + JSON.stringify(newEventObj));
if (myArgs.postUrl == null){ if (config.postUrl == null){
if (client.readyState === WebSocket.OPEN) { if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(newEventObj)); client.send(JSON.stringify(newEventObj));
} }
} else { } else {
request.post({ request.post({
url: myArgs.postUrl, url: config.postUrl,
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
...@@ -126,10 +143,10 @@ var sendJson = function(){ ...@@ -126,10 +143,10 @@ var sendJson = function(){
index = ++index % jsonFile.length; index = ++index % jsonFile.length;
} }
fs.readFile(myArgs.f, (err, data) => { fs.readFile(config.file, (err, data) => {
if (err) throw err; if (err) throw err;
jsonFile = JSON.parse(data); jsonFile = JSON.parse(data);
console.log('finished reading: ' + myArgs.f); console.log('finished reading: ' + config.file);
}); });
wss.on('connection', function connection(ws, req) { wss.on('connection', function connection(ws, req) {
...@@ -139,8 +156,8 @@ wss.on('connection', function connection(ws, req) { ...@@ -139,8 +156,8 @@ wss.on('connection', function connection(ws, req) {
ws.on('message', function incoming(message) { ws.on('message', function incoming(message) {
console.log('received: %s', message); console.log('received: %s', message);
if (myArgs.d > 0){ if (config.delay > 0){
var j = schedule.scheduleJob('*/' + myArgs.d + ' * * * * * *', sendJson); var j = schedule.scheduleJob('*/' + config.delay + ' * * * * * *', sendJson);
} else { } else {
client.send(JSON.stringify(jsonFile)); client.send(JSON.stringify(jsonFile));
} }
...@@ -149,7 +166,7 @@ wss.on('connection', function connection(ws, req) { ...@@ -149,7 +166,7 @@ wss.on('connection', function connection(ws, req) {
}); });
server.listen(myArgs.p, function listening() { server.listen(config.port, function listening() {
console.log('Listening on %d', server.address().port); console.log('Listening on %d', server.address().port);
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment