Browse Source

[util] Added template utils

master
Muthu Kumar 6 years ago
parent
commit
4012160d65
  1. 2
      package.json
  2. 14
      utils/cipherList.js
  3. 17
      utils/dedent.js
  4. 46
      utils/nginxPath.js
  5. 72
      utils/template.js

2
package.json

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

14
utils/cipherList.js

@ -0,0 +1,14 @@
'use strict';
module.exports = [
`ECDHE-ECDSA-AES256-GCM-SHA384`,
`ECDHE-RSA-AES256-GCM-SHA384`,
`ECDHE-ECDSA-CHACHA20-POLY1305`,
`ECDHE-RSA-CHACHA20-POLY1305`,
`ECDHE-ECDSA-AES128-GCM-SHA256`,
`ECDHE-RSA-AES128-GCM-SHA256`,
`ECDHE-ECDSA-AES256-SHA384`,
`ECDHE-RSA-AES256-SHA384`,
`ECDHE-ECDSA-AES128-SHA256`,
`ECDHE-RSA-AES128-SHA256`
];

17
utils/dedent.js

@ -0,0 +1,17 @@
'use strict';
const e = /[-\\^$*+?.()|[\]{}]/g;
const escape = s => s.replace(e, '\\$&');
const dedent = remove => (n = 1) => str =>
str
.split('\n')
.map(l => l.
replace(
new RegExp(`^${escape(remove).repeat(n)}`),
''
))
.join('\n');
module.exports = dedent;
module.exports.tabs = dedent('\t');
module.exports.spaces = dedent(' ');

46
utils/nginxPath.js

@ -10,41 +10,13 @@ const upPath = "/etc/up-serve/";
const wwwRoot = upPath + "static/"; const wwwRoot = upPath + "static/";
const serverListPath = upPath + "servers"; const serverListPath = upPath + "servers";
function nginxPath() { const nginxPath = () => npath;
return npath;
}
function enabledSites() {
return enabled;
}
function confD() {
return confDpath;
}
function webRoot() {
return wwwRoot;
}
function webRootDomain(domain, outPort) {
const path = wwwRoot + domain + "." + outPort;
return path;
}
function serversUp() {
const path = serverListPath + ".up";
return path;
}
function serversBakUp() {
const path = serverListPath + ".bak.up";
return path;
}
module.exports = nginxPath; module.exports = nginxPath;
module.exports.confD = confD; module.exports.enabledSites = () => enabled;
module.exports.enabledSites = enabledSites; module.exports.confD = () => confDpath;
module.exports.webRoot = webRoot; module.exports.webRoot = () => wwwRoot;
module.exports.webRootDomain = webRootDomain; module.exports.webRootDomain =
module.exports.serversUp = serversUp; (domain, outPort) =>
module.exports.serversBakUp = serversBakUp; wwwRoot + domain + '.' + outPort;
module.exports.serversUp = () => serverListPath + '.up';
module.exports.serversBakUp = () => serverListPath + '.bak.up';

72
utils/template.js

@ -0,0 +1,72 @@
'use strict';
const dedent = require('./dedent').tabs;
const npath = require('./nginxPath');
const ciperList = require('./cipherList');
const content = {
static: () => `try_files $uri $uri/ =404;`,
proxy: inPort => `proxy_pass http://localhost:${inPort};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;`,
ciperList: () => `'${ciperList.join(':')}'`,
ssl: (ssl, http2, hsts) => `listen 443 ssl ${http2 ? 'http2' : ''};
ssl_certificate ${ssl.ssl_certificate}
ssl_certificate_key ${ssl.ssl_certificate_key}
ssl_session_timeout 1440m;
ssl_session_cache shared:up_serve_nginx_SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2;
ssl_ciphers ${content.ciperList};
ssl_prefer_server_ciphers on;
${hsts /* If hsts is true, set to 6 months. If number, set it */
? `add_header Strict-Transport-Security max-age=`
+ typeof hsts === 'number' ? hsts : '15768000'
: '' /* By default don't set hsts */};
if ($scheme != "https") {
return 301 https://$host$request_uri;
}`,
};
console.log(content.get('static'));
const template = ({
outPort,
inPort,
domain,
type,
ssl,
http2,
hsts
}) => dedent(1)(`
# created by codefeathers/up-serve https://up.js.org
server {
${!ssl
/* Due to a security vulnerability in using gzip with SSL,
gzip is disabled for SSL. See: https://bugs.debian.org/773332 */
? `gzip on;
gzip_types text/css text/javascript image/svg+xml
application/vnd.ms-fontobject application/x-font-ttf
application/x-javascript application/javascript`
: ``};
listen ${outPort};
listen [::]:${outPort};
root ${npath.webRoot()}${domain}.${outPort};
index index.html index.htm;
server_name ${domain};
location / {
${content[type](inPort)}
}
${ssl ? content.ssl(ssl, http2, hsts) : ''}
}`);
module.exports = template;
Loading…
Cancel
Save