Browse Source

Append port number to config files and webroot

tags/v0.2.0
Muthu Kumar 7 years ago
parent
commit
7c50cdf8f2
  1. 41
      actions/createProxyServer.js
  2. 16
      actions/createStaticServer.js
  3. 8
      actions/killServer.js
  4. 1
      dev.com.undefined
  5. 10
      index.js
  6. 2
      package.json
  7. 4
      util/nginxConf.js
  8. 21
      util/nginxPath.js

41
actions/createProxyServer.js

@ -8,28 +8,27 @@ var nginxReload = require('../util/nginxReload');
var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain)),
"server {" + EOL +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
" root /var/www/" + domain + ";" + EOL +
" index index.html index.htm;" + EOL +
"" + EOL +
" server_name " + domain + ";" + EOL +
" location / {" + EOL +
" proxy_pass http://localhost:" + inPort + ";" + EOL +
" proxy_http_version 1.1;" + EOL +
" proxy_set_header Upgrade $http_upgrade;" + EOL +
" proxy_set_header Connection 'upgrade';" + EOL +
" proxy_set_header Host $host;" + EOL +
" proxy_cache_bypass $http_upgrade;" + EOL +
" }" + EOL +
"}"
)
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)); // Symlink the conf file from sites-available to sites-enabled
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)),
"server {" + EOL +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
" index index.html index.htm;" + EOL +
"" + EOL +
" server_name " + domain + ";" + EOL +
" location / {" + EOL +
" proxy_pass http://localhost:" + inPort + ";" + EOL +
" proxy_http_version 1.1;" + EOL +
" proxy_set_header Upgrade $http_upgrade;" + EOL +
" proxy_set_header Connection 'upgrade';" + EOL +
" proxy_set_header Host $host;" + EOL +
" proxy_cache_bypass $http_upgrade;" + EOL +
" }" + EOL +
"}"
)
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
nginxReload();
nginxReload();
};
module.exports = createProxyServer;

16
actions/createStaticServer.js

@ -10,25 +10,25 @@ var currentPath = path.normalize(process.cwd());
var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort = 80) {
fs.outputFileSync((conf(npath.availableSites(), domain)), // Gets nginx's paths from nginxPath.js
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js
"server {" + EOL +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
" root " + npath.webRoot() + domain + ";" + EOL +
" index index.html index.htm;" + EOL +
"" + EOL +
"" + EOL +
" server_name " + domain + ";" + EOL +
" location / {" + EOL +
" location / {" + EOL +
" try_files $uri $uri/ =404;" + EOL +
" }" + EOL +
" }" + EOL +
"}"
)
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist
shell.rm('-rf', conf(npath.enabledSites(), domain)); // Removes domain from sites-enabled if exists
shell.ln('-sf', conf(npath.availableSites(), domain), conf(npath.enabledSites(), domain)); // Symlink the conf file from sites-available to sites-enabled
shell.rm('-rf', npath.webRoot() + domain); // Removes domain from webroot 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.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.webRoot() + domain); // Symlink current directory to nginx's web root
shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root
nginxReload();
};

8
actions/killServer.js

@ -4,10 +4,10 @@ var npath = require('../util/nginxPath');
var conf = require('../util/nginxConf');
var nginxReload = require('../util/nginxReload');
function killServer(domain) {
shell.rm('-rf', conf(npath.enabledSites(), domain));
shell.rm('-rf', conf(npath.availableSites(), domain));
shell.rm('-rf', npath.webRoot() + domain);
function killServer(domain, outPort) {
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort));
shell.rm('-rf', conf(npath.availableSites(), domain, outPort));
shell.rm('-rf', npath.webRootDomain(domain, outPort));
nginxReload();
}

1
dev.com.undefined

@ -0,0 +1 @@
Subproject commit 965be8551443f1a797ec528eb37458547f895d1b

10
index.js

@ -16,10 +16,10 @@ var createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer');
// 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`
program
.version('0.1.2');
.version('0.1.5');
program
.command('static <domain> [outPort]')
@ -47,10 +47,10 @@ program
});
program
.command('kill <domain>')
.command('kill <domain> [ourPort]')
.description('Kill a server.')
.action(function (domain) {
killServer(domain);
.action(function (domain, outPort = 80) {
killServer(domain, outPort);
console.log("Done! Your server has been killed!\n");
});

2
package.json

@ -1,6 +1,6 @@
{
"name": "up-serve",
"version": "0.1.4",
"version": "0.1.5",
"description": "A cli tool to quickly create and manage nginx server blocks.",
"main": "index.js",
"scripts": {

4
util/nginxConf.js

@ -1,7 +1,7 @@
// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it.
function conf(path, domain) {
return (path + domain + ".conf");
function conf(path, domain, outPort) {
return (path + domain + "." + outPort + ".conf");
}
module.exports = conf;

21
util/nginxPath.js

@ -1,20 +1,27 @@
// 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 wwwRoot = "/var/www/";
function availableSites() {
var availableSites = "/etc/nginx/sites-available/";
return availableSites;
return available;
}
function enabledSites() {
var enabledSites = "/etc/nginx/sites-enabled/";
return enabledSites;
return enabled;
}
function webRoot() {
var webRoot = "/var/www/";
return webRoot;
return wwwRoot;
}
function webRootDomain(domain, outPort) {
rootWithDomain = wwwRoot + domain + "." + outPort;
return rootWithDomain;
}
module.exports.availableSites = availableSites;
module.exports.enabledSites = enabledSites;
module.exports.webRoot = webRoot;
module.exports.webRoot = webRoot;
module.exports.webRootDomain = webRootDomain;
Loading…
Cancel
Save