diff --git a/src/nginxconfig/generators/conf/nginx.conf.js b/src/nginxconfig/generators/conf/nginx.conf.js index d17281f..d01e715 100644 --- a/src/nginxconfig/generators/conf/nginx.conf.js +++ b/src/nginxconfig/generators/conf/nginx.conf.js @@ -1,4 +1,5 @@ import sslProfiles from '../../util/ssl_profiles'; +import websiteConf from './website.conf'; export default (domains, global) => { const config = {}; @@ -105,7 +106,10 @@ export default (domains, global) => { // Single file configs if (!global.tools.modularizedStructure.computed) { - // TODO: figure out merging in all the other configs + for (const domain of domains) { + config.http.push([`# ${domain.server.domain.computed}`, '']); + config.http.push(...websiteConf(domain, domains, global)); + } } // Done! diff --git a/src/nginxconfig/generators/conf/website.conf.js b/src/nginxconfig/generators/conf/website.conf.js index 2cefdc9..8815e5f 100644 --- a/src/nginxconfig/generators/conf/website.conf.js +++ b/src/nginxconfig/generators/conf/website.conf.js @@ -132,7 +132,7 @@ export default (domain, domains, global) => { && (!domain.reverseProxy.reverseProxy.computed || domain.reverseProxy.path.computed !== '/')) { serverConfig.push([`# index.${domain.routing.fallbackHtml.computed ? 'html' : (domain.routing.fallbackPhp.computed ? 'php' : '')} fallback`, '']); serverConfig.push(['location /', { - try_files: `$uri $uri/ /index.${domain.routing.fallbackHtml.computed ? '.html' : (domain.routing.fallbackPhp.computed ? '.php?$query_string' : '')}`, + try_files: `$uri $uri/ /index.${domain.routing.fallbackHtml.computed ? 'html' : (domain.routing.fallbackPhp.computed ? 'php?$query_string' : '')}`, }]); } diff --git a/src/nginxconfig/generators/to_conf.js b/src/nginxconfig/generators/to_conf.js index af50a00..4a9dec6 100644 --- a/src/nginxconfig/generators/to_conf.js +++ b/src/nginxconfig/generators/to_conf.js @@ -1,5 +1,27 @@ import isObject from '../util/is_object'; +const isBlock = item => { + // If an object, or kv entries, this is considered a block + return isObject(item) || (Array.isArray(item) && item.every(i => Array.isArray(i) && i.length === 2)); +}; + +const longestKey = items => { + let longest = 0; + + for (const item of items) { + // Only consider up to the first block + if (isBlock(item[1])) + return longest; + + // If this is the new longest, and not a comment, use this + if (item[0].length > longest && !item[0].startsWith('#')) + longest = item[0].length; + } + + // Done! + return longest; +}; + const recurse = (entriesOrObject, depth) => { // Support an object or kv array entries // Convert to entries if given an object @@ -10,32 +32,40 @@ const recurse = (entriesOrObject, depth) => { // Initial values let retVal = ''; - const longestKeyLen = entries.reduce((prev, current) => { - if (!current[0].startsWith('#') && current[0].length > prev) - return current[0].length; - return prev; - }, 0); + let longestKeyLen = longestKey(entries); const indent = (' ').repeat(depth); + // Track whether the previous was a block, for indentation + let previousBlock = false; + // Loop over every kv pair - for (const item of entries) { - // If an object, or kv entries, recurse - if (isObject(item[1]) || (Array.isArray(item[1]) && item[1].every(i => Array.isArray(i) && i.length === 2))) { + for (let i = 0; i < entries.length; i++) { + const item = entries[i]; + + // If a block (object or kv entries), recurse + if (isBlock(item[1])) { // Recurse retVal += '\n' + indent + item[0] + ' {\n'; retVal += recurse(item[1], depth + 1); retVal += indent + '}\n\n'; // Done + previousBlock = true; continue; } + // Update key length if we've just left a block + if (previousBlock) { + longestKeyLen = longestKey(entries.slice(i)); + previousBlock = false; + } + // Otherwise, assume it can be made a string // Ensure we're working with an array const val = Array.isArray(item[1]) ? item[1] : [item[1]]; // Calculate spacing - const keyValSpacing = (longestKeyLen - item[0].length) + 4; + const keyValSpacing = (longestKeyLen - item[0].length) + 1; const keyValIndent = (' ').repeat(Math.max(keyValSpacing, 0)); // Work through each item in the array