Browse Source

Fixed `servers.up` parsing issues.

Pending: Handle errors if `servers.up` was manually modified.
pull/6/head
Muthu Kumar 7 years ago
parent
commit
7d83c727d0
  1. 56
      utils/listFile.js
  2. 3
      utils/removeFromArray.js

56
utils/listFile.js

@ -1,63 +1,45 @@
var fs = require('fs-extra');
var shell = require('shelljs');
var removeFromArray = require('./removeFromArray');
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
var listFilePath = "./servers.up";
var listFilePath = "/etc/up-serve/servers.up";
function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
var jsonFile = { "domains": [] };
var domBlock;
var domBlock = {
"domain": domain,
"outPort": outPort
};
if (!inPort) {
domBlock = {
"domain": domain,
"type": "static",
"outPort": outPort
};
domBlock.type = "static";
} else {
domBlock = {
"domain": domain,
"type": "proxy",
"outPort": outPort,
"inPort": inPort
};
domBlock.type = "proxy";
domBlock.inPort = inPort;
}
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
/*for (name in jsonFile) {
if (name == domain && jsonFile[name][outPort] == outPort) {
delete jsonFile[domain];
console.log('\nDomain was deleted successfully.\n');
}
}*/
jsonFile = removeFromArray(jsonFile.domains, domain, outPort);
if (fs.existsSync(listFilePath)) {
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
console.log(jsonFile);
}
console.log(jsonFile);
console.log(jsonFile.domains);
jsonFile.domains.push(domBlock);
console.log(jsonFile);
jsonFile = JSON.stringify(jsonFile, null, '\t');
console.log(jsonFile);
fs.writeFileSync(listFilePath, jsonFile);
}
function removeFromList (domain, outPort) {
var jsonFile = {};
var jsonFile = { "domains": [] };
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
// for (name in jsonFile) {
// if (name == domain && jsonFile[domain].outPort == outPort) {
// delete jsonFile[domain];
// console.log('\nDomain was deleted successfully.\n');
// }
// }
jsonFile = removeFromArray(jsonFile.domains, domain, outPort);
jsonFile = JSON.stringify(jsonFile, null, '\t');
@ -68,7 +50,7 @@ function removeFromList (domain, outPort) {
function readServers () {
var serversList = JSON.parse(fs.readFileSync(listFilePath));
if(!serversList[0]) return undefined;
if(!serversList.domains[0]) return undefined;
return serversList;
}

3
utils/removeFromArray.js

@ -5,9 +5,6 @@ function removeFromArray (arr, dom, port) {
if (shouldDelete[0]) {
arr.splice(shouldDelete[1], 1);
console.log('\nItem was deleted!\n');
} else {
console.log('\nItem was not found!\n');
}
return arr;
}

Loading…
Cancel
Save