From 9119a94627282c8c657f332ca4b9c0cab42e47be Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Mon, 30 Oct 2017 04:16:33 +0530 Subject: [PATCH] Working on static server --- actions/createProxyServer.js | 8 +++---- actions/createStaticServer.js | 18 +++++++++++++++ index.js | 53 ++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 actions/createStaticServer.js diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index dd50977..6ff4724 100644 --- a/actions/createProxyServer.js +++ b/actions/createProxyServer.js @@ -1,16 +1,16 @@ var fs = require('fs-extra') -function createProxyServer(domain, port) { +function createProxyServer(domain, inPort) { fs.outputFileSync("/test.txt", "server {" + "\n" + - " listen 80;" + "\n" + - " listen [::]:80;" + "\n" + + " listen " + outPort + ";" + "\n" + + " listen [::]:" + outPort + ";" + "\n" + " root /var/www/" + domain + ";" + "\n" + " index index.html index.htm;" + "\n" + "" + "\n" + " server_name " + domain + "\n" + " location / {" + "\n" + - " proxy_pass http://localhost:" + port + ";" + "\n" + + " proxy_pass http://localhost:" + inPort + ";" + "\n" + " proxy_http_version 1.1;" + "\n" + " proxy_set_header Upgrade $http_upgrade;" + "\n" + " proxy_set_header Connection 'upgrade';" + "\n" + diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js new file mode 100644 index 0000000..8a2e03d --- /dev/null +++ b/actions/createStaticServer.js @@ -0,0 +1,18 @@ +var fs = require('fs-extra') + +function createStaticServer(domain, port) { + fs.outputFileSync("/test.txt", + "server {" + "\n" + + " listen 80;" + "\n" + + " listen [::]:80;" + "\n" + + " root /var/www/" + domain + ";" + "\n" + + " index index.html index.htm;" + "\n" + + "" + "\n" + + " server_name " + domain + "\n" + + " location / {" + "\n" + + " try_files $uri $uri/ =404;" + + "}" + ) +} + +module.exports = createProxyServer \ No newline at end of file diff --git a/index.js b/index.js index 9af6354..82a79eb 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,32 @@ #!/usr/bin/env node --harmony +// Requiring npm modules var program = require('commander') var shell = require('shelljs') var fs = require('fs-extra') var chalk = require('chalk') var validator = require('validator') + +// Requiring Actions var createProxyServer = require('./actions/createProxyServer') -var isFQDN = validator.isFQDN -/* Use in prod -var isWin = /^win/.test(process.platform) +// +var isFQDN = validator.isFQDN -if(isWin == "Win32") +/* +//Detect Linux or BSD +var isLin = /^linux|^bsd/.test(process.platform) -if (!shell.which('node')) { - shell.echo('I need NodeJS to work. Install nginx first. https://nodejs.org/'); - shell.exit(1); +//Throw if OS is not Linux or BSD +if(!isLin) { + shell.echo("\nThis is not a Linux or freeBSD distribution. I'm not written for this distro. Please raise an issue at " + chalk.cyan("https://github.com/codefeathers/up-serve") + " if you want `up` to be ported for your distro") + shell.exit(1) } +//Throw if Nginx is not found if (!shell.which('nginx')) { - shell.echo('I need nginx to work. Install nginx first. https://nginx.org/'); - shell.exit(1); + shell.echo('I need nginx to work. Install nginx first. https://nginx.org/') + shell.exit(1) } */ @@ -28,27 +34,32 @@ program .version('0.0.1') program - .command('static [relativePath]') + .command('static [relativePath] [outPort]') .description('Create a static server at this folder.') - .action(function(domain) { + .action(function(domain, relativePath="", outPort="") { if(!isFQDN(domain)) console.log('\nDomain is not valid. Please use a valid domain name.') // Stuff happens here }) program - .command('proxy ') + .command('proxy [outPort]') .description('Create a proxy server, listening at port number.') - .action(function(domain, port) { - if(!isFQDN(domain)) + .action(function(domain, inPort, outPort = "") { + var validInPort = /^\d+$/.test(inPort) + var validOutPort = /^\d+$/.test(outPort) + if(!isFQDN(domain)) { console.log('\nDomain is not valid. Please use a valid domain name.') - else if(typeof port !== 'number') + return; } + if(!validInPort || !validOutPort) { + if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) { + console.log('\nPort should be a number from 1 and 65535.') + return; } console.log('\nPort should be a number.') - // Stuff happens here - //test stuff will be deleted later: - else - createProxyServer(domain, port) + return; } + else { + //createProxyServer(domain, inPort) console.log('Done!') - //test stuff ends here + } }) program @@ -68,7 +79,7 @@ program program .command('*') .action(function() { - console.log('Invalid command. Type "up --help" for help.') + console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help.") }) program.on('--help', function(){