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

3
utils/removeFromArray.js

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

Loading…
Cancel
Save