Browse Source

Working on adding and removing list items

pull/3/head
Muthu Kumar 7 years ago
parent
commit
f8553d78cb
  1. 6
      actions/createProxyServer.js
  2. 5
      actions/createStaticServer.js
  3. 2
      actions/killServer.js
  4. 24
      index.js
  5. 19
      package-lock.json
  6. 1
      package.json
  7. 31
      utils/listFile.js
  8. 12
      utils/nginxPath.js

6
actions/createProxyServer.js

@ -4,11 +4,12 @@ var shell = require('shelljs');
var npath = require('../utils/nginxPath'); var npath = require('../utils/nginxPath');
var conf = require('../utils/nginxConf'); var conf = require('../utils/nginxConf');
var nginxReload = require('../utils/nginxReload'); var nginxReload = require('../utils/nginxReload');
var appendToList = require('../utils/listFile').appendToList;
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) { function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), fs.outputFileSync((conf(npath.confD(), domain, outPort)),
"server {" + EOL + "server {" + EOL +
" listen " + outPort + ";" + EOL + " listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL +
@ -26,8 +27,9 @@ function createProxyServer(domain, inPort, outPort) {
"}" "}"
); );
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
appendToList(domain, outPort, inPort);
nginxReload(); nginxReload();
} }

5
actions/createStaticServer.js

@ -11,7 +11,7 @@ var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort) { function createStaticServer(domain, outPort) {
outPort = outPort || 80; outPort = outPort || 80;
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js fs.outputFileSync((conf(npath.confD(), domain, outPort)), // Gets nginx's paths from nginxPath.js
"server {" + EOL + "server {" + EOL +
" listen " + outPort + ";" + EOL + " listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL +
@ -26,11 +26,12 @@ function createStaticServer(domain, outPort) {
); );
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists
shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists
shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail
shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root
appendToList(domain, outPort);
nginxReload(); nginxReload();
} }

2
actions/killServer.js

@ -6,7 +6,7 @@ var nginxReload = require('../utils/nginxReload');
function killServer(domain, outPort) { function killServer(domain, outPort) {
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); shell.rm('-rf', conf(npath.enabledSites(), domain, outPort));
shell.rm('-rf', conf(npath.availableSites(), domain, outPort)); shell.rm('-rf', conf(npath.confD(), domain, outPort));
shell.rm('-rf', npath.webRootDomain(domain, outPort)); shell.rm('-rf', npath.webRootDomain(domain, outPort));
nginxReload(); nginxReload();

24
index.js

@ -3,17 +3,22 @@
// Requiring npm modules // Requiring npm modules
var program = require('commander'); var program = require('commander');
var chalk = require('chalk'); var chalk = require('chalk');
var fs = require('fs-extra');
// Requiring utils var prettyjson = require('prettyjson');
var validate = require('./utils/validate');
var requirements = require('./utils/requirements');
var appendToList = require('./utils/listFile');
// Requiring Actions // Requiring Actions
var createProxyServer = require('./actions/createProxyServer'); var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer'); var createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer'); var killServer = require('./actions/killServer');
// Requiring utils
var validate = require('./utils/validate');
var requirements = require('./utils/requirements');
var appendToList = require('./utils/listFile').appendToList;
var readServers = require('./utils/listFile').readServers;
var EOL = require('os').EOL;
// Check for requirements such as OS version and nginx install. Throw and exit if requirements not found. #Roadmap: Add ability to satisfy any possible requirements. // Check for requirements such as OS version and nginx install. Throw and exit if requirements not found. #Roadmap: Add ability to satisfy any possible requirements.
requirements(); // Comment in development and uncomment this line in production. This should check whether the OS is compatible with this version of `up` requirements(); // Comment in development and uncomment this line in production. This should check whether the OS is compatible with this version of `up`
@ -28,7 +33,7 @@ program
if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns. if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns.
createStaticServer(domain, outPort); createStaticServer(domain, outPort);
if (outPort != "80" || "443") domain = domain + ":" + outPort; if (outPort != "80" || "443") domain = domain + ":" + outPort;
console.log("Done! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); console.log("\nDone! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!");
}); });
program program
@ -39,14 +44,15 @@ program
if (!validate(domain, inPort, outPort)) return; if (!validate(domain, inPort, outPort)) return;
createProxyServer(domain, inPort, outPort); createProxyServer(domain, inPort, outPort);
if (outPort != "80" || "443") domain = domain + ":" + outPort; if (outPort != "80" || "443") domain = domain + ":" + outPort;
console.log("Done! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); console.log("\nDone! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!");
}); });
program program
.command('list') .command('list')
.description('List all available servers.') .description('List all available servers.')
.action(function () { .action(function () {
// Stuff happens here var serversList = readServers();
console.log(EOL + prettyjson.render(serversList) + EOL);
}); });
program program
@ -61,7 +67,7 @@ program
program program
.command('*') // This should pick invalid commands, but it doesn't, yet. .command('*') // This should pick invalid commands, but it doesn't, yet.
.action(function () { .action(function () {
console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help."); console.log("\nInvalid command. Type " + chalk.cyan('up --help') + " for help.\n");
}); });
// Adds custom help text to the automatically generated help. // Adds custom help text to the automatically generated help.

19
package-lock.json

@ -49,6 +49,11 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
}, },
"colors": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz",
"integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM="
},
"commander": { "commander": {
"version": "2.11.0", "version": "2.11.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",
@ -142,6 +147,11 @@
"brace-expansion": "1.1.8" "brace-expansion": "1.1.8"
} }
}, },
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
},
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@ -160,6 +170,15 @@
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz",
"integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=" "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME="
}, },
"prettyjson": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/prettyjson/-/prettyjson-1.2.1.tgz",
"integrity": "sha1-/P+rQdGcq0365eV15kJGYZsS0ok=",
"requires": {
"colors": "1.1.2",
"minimist": "1.2.0"
}
},
"rechoir": { "rechoir": {
"version": "0.6.2", "version": "0.6.2",
"resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz",

1
package.json

@ -31,6 +31,7 @@
"commander": "^2.11.0", "commander": "^2.11.0",
"fs-extra": "^4.0.2", "fs-extra": "^4.0.2",
"json-beautify": "^1.0.1", "json-beautify": "^1.0.1",
"prettyjson": "^1.2.1",
"shelljs": "^0.7.8", "shelljs": "^0.7.8",
"validator": "^9.1.1" "validator": "^9.1.1"
} }

31
utils/listFile.js

@ -8,6 +8,7 @@ var listFilePath = "/etc/up-serve/servers.up";
function appendToList(domain, outPort, inPort) { function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
var jsonFile = {}; var jsonFile = {};
var domBlock; var domBlock;
@ -43,8 +44,34 @@ function appendToList(domain, outPort, inPort) {
jsonFile[domain] = domBlock; jsonFile[domain] = domBlock;
jsonFile = beautifyJSON(jsonFile); jsonFile = beautifyJSON(jsonFile);
} }
fs.writeFileSync(listFilePath, jsonFile); fs.writeFileSync(listFilePath, jsonFile);
} }
module.exports = appendToList; function readServers () {
return JSON.parse(fs.readFileSync(listFilePath));
}
function removeFromList (domain, outPort) {
var jsonFile = {};
if (fs.existsSync(listFilePath)) {
jsonFile = fs.readFileSync(listFilePath);
jsonFile = JSON.parse(jsonFile);
for (block in jsonFile) {
if (block.domain == domain && block.domain.outPort == outPort) {
delete jsonFile.block;
return;
}
}
jsonFile = beautifyJSON(jsonFile, null, 2, 30);
}
else {
console.log("\Domain was not in my list. Are you sure?\n")
}
fs.writeFileSync(listFilePath, jsonFile);
}
module.exports.appendToList = appendToList;
module.exports.readServers = readServers;

12
utils/nginxPath.js

@ -1,17 +1,17 @@
// These functions just return paths. Later, these should be modified to poll from nginx's config. // These functions just return paths. Later, these should be modified to poll from nginx's config.
var available = "/etc/nginx/sites-available/";
var enabled = "/etc/nginx/sites-enabled/"; var enabled = "/etc/nginx/sites-enabled/";
var conf = "/etc/nginx/conf.d/";
var wwwRoot = "/var/www/"; var wwwRoot = "/var/www/";
function availableSites() {
return available;
}
function enabledSites() { function enabledSites() {
return enabled; return enabled;
} }
function confD() {
return conf;
}
function webRoot() { function webRoot() {
return wwwRoot; return wwwRoot;
} }
@ -21,7 +21,7 @@ function webRootDomain(domain, outPort) {
return rootWithDomain; return rootWithDomain;
} }
module.exports.availableSites = availableSites; module.exports.confD = confD;
module.exports.enabledSites = enabledSites; module.exports.enabledSites = enabledSites;
module.exports.webRoot = webRoot; module.exports.webRoot = webRoot;
module.exports.webRootDomain = webRootDomain; module.exports.webRootDomain = webRootDomain;
Loading…
Cancel
Save