Muthu Kumar
7 years ago
8 changed files with 148 additions and 121 deletions
@ -0,0 +1,21 @@ |
|||||
|
{ |
||||
|
"env": { |
||||
|
"node": true |
||||
|
}, |
||||
|
"extends": "eslint:recommended", |
||||
|
"rules": { |
||||
|
"no-console": "off", |
||||
|
"indent": [ |
||||
|
"error", |
||||
|
"tab" |
||||
|
], |
||||
|
"linebreak-style": [ |
||||
|
"error", |
||||
|
"unix" |
||||
|
], |
||||
|
"semi": [ |
||||
|
"error", |
||||
|
"always" |
||||
|
] |
||||
|
} |
||||
|
} |
@ -1,34 +1,34 @@ |
|||||
var fs = require('fs-extra'); |
var fs = require('fs-extra'); |
||||
var shell = require('shelljs'); |
var shell = require('shelljs'); |
||||
|
|
||||
var npath = require('../util/nginxPath'); |
var npath = require('../util/nginxPath'); |
||||
var conf = require('../util/nginxConf'); |
var conf = require('../util/nginxConf'); |
||||
var nginxReload = require('../util/nginxReload'); |
var nginxReload = require('../util/nginxReload'); |
||||
|
|
||||
var { EOL } = require('os'); // \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.availableSites(), domain, outPort)), |
||||
"server {" + EOL + |
"server {" + EOL + |
||||
" listen " + outPort + ";" + EOL + |
" listen " + outPort + ";" + EOL + |
||||
" listen [::]:" + outPort + ";" + EOL + |
" listen [::]:" + outPort + ";" + EOL + |
||||
" index index.html index.htm;" + EOL + |
" index index.html index.htm;" + EOL + |
||||
"" + EOL + |
"" + EOL + |
||||
" server_name " + domain + ";" + EOL + |
" server_name " + domain + ";" + EOL + |
||||
" location / {" + EOL + |
" location / {" + EOL + |
||||
" proxy_pass http://localhost:" + inPort + ";" + EOL + |
" proxy_pass http://localhost:" + inPort + ";" + EOL + |
||||
" proxy_http_version 1.1;" + EOL + |
" proxy_http_version 1.1;" + EOL + |
||||
" proxy_set_header Upgrade $http_upgrade;" + EOL + |
" proxy_set_header Upgrade $http_upgrade;" + EOL + |
||||
" proxy_set_header Connection 'upgrade';" + EOL + |
" proxy_set_header Connection 'upgrade';" + EOL + |
||||
" proxy_set_header Host $host;" + EOL + |
" proxy_set_header Host $host;" + EOL + |
||||
" proxy_cache_bypass $http_upgrade;" + EOL + |
" proxy_cache_bypass $http_upgrade;" + EOL + |
||||
" }" + EOL + |
" }" + EOL + |
||||
"}" |
"}" |
||||
) |
); |
||||
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.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
|
||||
|
|
||||
nginxReload(); |
nginxReload(); |
||||
}; |
} |
||||
|
|
||||
module.exports = createProxyServer; |
module.exports = createProxyServer; |
@ -1,36 +1,37 @@ |
|||||
var fs = require('fs-extra'); |
var fs = require('fs-extra'); |
||||
var shell = require('shelljs'); |
var shell = require('shelljs'); |
||||
var path = require('path'); |
var path = require('path'); |
||||
|
|
||||
var npath = require('../util/nginxPath'); |
var npath = require('../util/nginxPath'); |
||||
var conf = require('../util/nginxConf'); |
var conf = require('../util/nginxConf'); |
||||
var nginxReload = require('../util/nginxReload'); |
var nginxReload = require('../util/nginxReload'); |
||||
|
|
||||
var currentPath = path.normalize(process.cwd()); |
var currentPath = path.normalize(process.cwd()); |
||||
var { EOL } = require('os'); // \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 createStaticServer(domain, outPort = 80) { |
function createStaticServer(domain, outPort) { |
||||
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js
|
outPort = outPort || 80; |
||||
"server {" + EOL + |
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js
|
||||
" listen " + outPort + ";" + EOL + |
"server {" + EOL + |
||||
" listen [::]:" + outPort + ";" + EOL + |
" listen " + outPort + ";" + EOL + |
||||
" root " + npath.webRoot() + domain + ";" + EOL + |
" listen [::]:" + outPort + ";" + EOL + |
||||
" index index.html index.htm;" + EOL + |
" root " + npath.webRoot() + domain + ";" + EOL + |
||||
"" + EOL + |
" index index.html index.htm;" + EOL + |
||||
" server_name " + domain + ";" + EOL + |
"" + EOL + |
||||
" location / {" + EOL + |
" server_name " + domain + ";" + EOL + |
||||
" try_files $uri $uri/ =404;" + EOL + |
" location / {" + EOL + |
||||
" }" + EOL + |
" try_files $uri $uri/ =404;" + EOL + |
||||
"}" |
" }" + EOL + |
||||
) |
"}" |
||||
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.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.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists
|
||||
shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot 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.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail
|
shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists
|
||||
shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root
|
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
|
||||
nginxReload(); |
|
||||
}; |
nginxReload(); |
||||
|
} |
||||
|
|
||||
module.exports = createStaticServer; |
module.exports = createStaticServer; |
@ -1,27 +1,27 @@ |
|||||
// 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 available = "/etc/nginx/sites-available/"; |
||||
var enabled = "/etc/nginx/sites-enabled/"; |
var enabled = "/etc/nginx/sites-enabled/"; |
||||
var wwwRoot = "/var/www/"; |
var wwwRoot = "/var/www/"; |
||||
|
|
||||
function availableSites() { |
function availableSites() { |
||||
return available; |
return available; |
||||
} |
} |
||||
|
|
||||
function enabledSites() { |
function enabledSites() { |
||||
return enabled; |
return enabled; |
||||
} |
} |
||||
|
|
||||
function webRoot() { |
function webRoot() { |
||||
return wwwRoot; |
return wwwRoot; |
||||
} |
} |
||||
|
|
||||
function webRootDomain(domain, outPort) { |
function webRootDomain(domain, outPort) { |
||||
rootWithDomain = wwwRoot + domain + "." + outPort; |
var rootWithDomain = wwwRoot + domain + "." + outPort; |
||||
return rootWithDomain; |
return rootWithDomain; |
||||
} |
} |
||||
|
|
||||
module.exports.availableSites = availableSites; |
module.exports.availableSites = availableSites; |
||||
module.exports.enabledSites = enabledSites; |
module.exports.enabledSites = enabledSites; |
||||
module.exports.webRoot = webRoot; |
module.exports.webRoot = webRoot; |
||||
module.exports.webRootDomain = webRootDomain; |
module.exports.webRootDomain = webRootDomain; |
@ -1,14 +1,14 @@ |
|||||
var { execSync } = require('child_process'); |
var execSync = require('child_process').execSync; |
||||
|
|
||||
function nginxReload() { |
function nginxReload() { |
||||
execSync('service nginx reload', function (error, stdout, stderr) { |
execSync('service nginx reload', function (error, stdout, stderr) { |
||||
if (error) { |
if (error) { |
||||
console.error(`exec error: ${error}`); |
console.error("exec error: " + error); |
||||
console.log(`stdout: ${stdout}`); |
console.log("stdout: " + stdout); |
||||
console.log(`stderr: ${stderr}`); |
console.log("stderr: " + stderr); |
||||
return; |
return; |
||||
} |
} |
||||
}) |
}); |
||||
} |
} |
||||
|
|
||||
module.exports = nginxReload; |
module.exports = nginxReload; |
Loading…
Reference in new issue