Browse Source

Merge pull request #3 from codefeathers/up-list

Feature branch `up list`, merging to `develop`
pull/5/head
Muthu Kumar 7 years ago
committed by GitHub
parent
commit
824c5bce21
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      actions/createProxyServer.js
  2. 6
      actions/createStaticServer.js
  3. 4
      actions/killServer.js
  4. 23
      index.js
  5. 19
      package-lock.json
  6. 2
      package.json
  7. 78
      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 conf = require('../utils/nginxConf');
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.
function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)),
fs.outputFileSync((conf(npath.confD(), domain, outPort)),
"server {" + 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.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();
}

6
actions/createStaticServer.js

@ -5,13 +5,14 @@ var path = require('path');
var npath = require('../utils/nginxPath');
var conf = require('../utils/nginxConf');
var nginxReload = require('../utils/nginxReload');
var appendToList = require('../utils/listFile').appendToList;
var currentPath = path.normalize(process.cwd());
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort) {
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 +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
@ -26,11 +27,12 @@ function createStaticServer(domain, outPort) {
);
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.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.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
appendToList(domain, outPort);
nginxReload();
}

4
actions/killServer.js

@ -3,13 +3,15 @@ var shell = require('shelljs');
var npath = require('../utils/nginxPath');
var conf = require('../utils/nginxConf');
var nginxReload = require('../utils/nginxReload');
var removeFromList = require('../utils/listFile').removeFromList;
function killServer(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));
nginxReload();
removeFromList(domain, outPort);
}
module.exports = killServer;

23
index.js

@ -3,18 +3,24 @@
// Requiring npm modules
var program = require('commander');
var chalk = require('chalk');
// Requiring utils
var validate = require('./utils/validate');
var requirements = require('./utils/requirements');
var fs = require('fs-extra');
var prettyjson = require('prettyjson');
// Requiring Actions
var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer');
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.
requirements(); // Comment in development and uncomment this line in production. This should check whether the OS is compatible with this version of `up`
program
@ -28,7 +34,7 @@ program
if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns.
createStaticServer(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
@ -39,14 +45,15 @@ program
if (!validate(domain, inPort, outPort)) return;
createProxyServer(domain, inPort, 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
.command('list')
.description('List all available servers.')
.action(function () {
// Stuff happens here
var serversList = readServers();
console.log(EOL + prettyjson.render(serversList) + EOL);
});
program
@ -61,7 +68,7 @@ program
program
.command('*') // This should pick invalid commands, but it doesn't, yet.
.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.

19
package-lock.json

@ -221,6 +221,11 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
},
"colors": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz",
"integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM="
},
"commander": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",
@ -598,6 +603,10 @@
"resolved": "https://registry.npmjs.org/interpret/-/interpret-1.0.4.tgz",
"integrity": "sha1-ggzdWIuGj/sZGoCVBtbJyPISsbA="
},
"json-beautify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/json-beautify/-/json-beautify-1.0.1.tgz",
"integrity": "sha1-WYtQ1Mjqm4/KWru0C34svTrUwvw="
"is-fullwidth-code-point": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
@ -747,6 +756,9 @@
}
},
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
@ -838,6 +850,13 @@
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz",
"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"
"pify": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz",

2
package.json

@ -30,6 +30,8 @@
"chalk": "^2.3.0",
"commander": "^2.11.0",
"fs-extra": "^4.0.2",
"json-beautify": "^1.0.1",
"prettyjson": "^1.2.1",
"shelljs": "^0.7.8",
"validator": "^9.1.1"
},

78
utils/listFile.js

@ -0,0 +1,78 @@
var fs = require('fs-extra');
var shell = require('shelljs');
var beautifyJSON = require("json-beautify");
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
var listFilePath = "/etc/up-serve/servers.up";
function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
var jsonFile = {};
var domBlock;
if (!inPort) {
domBlock = {
"type": "static",
"outPort": outPort,
"inPort": undefined
}
} else {
domBlock = {
"type": "proxy",
"outPort": outPort,
"inPort": inPort
}
}
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[domain] = domBlock;
jsonFile = beautifyJSON(jsonFile, null, 2, 30);
}
else {
jsonFile[domain] = domBlock;
jsonFile = beautifyJSON(jsonFile);
}
fs.writeFileSync(listFilePath, jsonFile);
}
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;
console.log('\nDomain was deleted successfully.\n');
return;
}
}
jsonFile = beautifyJSON(jsonFile, null, 2, 30);
}
else {
console.log("\Domain was not in my list. Are you sure?\n")
}
fs.writeFileSync(listFilePath, jsonFile);
}
function readServers () {
return JSON.parse(fs.readFileSync(listFilePath));
}
module.exports.appendToList = appendToList;
module.exports.readServers = readServers;
module.exports.removeFromList = removeFromList;

12
utils/nginxPath.js

@ -1,17 +1,17 @@
// 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 conf = "/etc/nginx/conf.d/";
var wwwRoot = "/etc/up-serve/static/";
function availableSites() {
return available;
}
function enabledSites() {
return enabled;
}
function confD() {
return conf;
}
function webRoot() {
return wwwRoot;
}
@ -21,7 +21,7 @@ function webRootDomain(domain, outPort) {
return rootWithDomain;
}
module.exports.availableSites = availableSites;
module.exports.confD = confD;
module.exports.enabledSites = enabledSites;
module.exports.webRoot = webRoot;
module.exports.webRootDomain = webRootDomain;
Loading…
Cancel
Save