From 9ea4dcbfbeeff6c14562793426cac6a4d292f968 Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@nsc.liu.se>
Date: Fri, 3 May 2024 20:43:51 +0200
Subject: [PATCH] Refactor generation of <VirtualHost> content.

Build up the config lines we generate for the <VirtualHost> container
in a list, and then insert that list line by line, instead of having
lots of <% ... %> and <%= ... %> sections.  I think this way may be a
bit easier to read when the ERB template gets more complicated.

We actually had a section of outcommented code that tried to do this.
That outcommented section had been in the file since I committed it
originally back in 2014.  I probably intended to remove it before I
checked in the file, but forgot to do so.  But that snippet prompted
me to do this refactor now...
---
 templates/named_vhost.conf.erb | 63 ++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 30 deletions(-)

diff --git a/templates/named_vhost.conf.erb b/templates/named_vhost.conf.erb
index 51321bf..66aa99e 100644
--- a/templates/named_vhost.conf.erb
+++ b/templates/named_vhost.conf.erb
@@ -1,5 +1,5 @@
 <%
-    # Copyright (C) 2014-2022 Thomas Bellman.
+    # Copyright (C) 2014-2024 Thomas Bellman.
     # Licensed under the GNU LGPL v3+; see the README file for more information.
 
     httpd_version = scope.lookupvar('apache::standard_version')
@@ -51,16 +51,37 @@
 	server_names = [@servernames].flatten
     end
     server_addrs.sort!.uniq!
--%>
-<%
-##    cfg = []
-##    cfg << 'ServerName ' + server_names[0]
-##    cfg += server_names[1..-1].collect { |aliasname|
-##	'ServerAlias ' + aliasname
-##    }
-##    if @documentroot && @documentroot != ''
-##	cfg << 'DocumentRoot ' + @documentroot
-##    end
+
+    # Config lines inside the <VirtualHost> container
+    vhostcfg = []
+
+    vhostcfg << 'ServerName ' + server_names[0]
+    vhostcfg += server_names[1..-1].collect { |aliasname|
+	'ServerAlias ' + aliasname
+    }
+
+    if @certname && @certname != ''
+	certdir = scope.lookupvar('x509certs::pki_certdir')
+	keydir  = scope.lookupvar('x509certs::pki_keydir')
+	certfile  = File.join(certdir, @certname + '-cert.pem')
+	keyfile   = File.join(keydir, @certname + '-key.pem')
+	chainfile = File.join(certdir, @certname + '-chain.pem')
+	vhostcfg += [
+	    '',
+	    'SSLEngine on',
+	    'SSLCertificateFile ' + certfile,
+	    'SSLCertificateKeyFile ' + keyfile,
+	    'SSLCertificateChainFile ' + chainfile,
+	]
+    end
+
+    if @documentroot && @documentroot != ''
+	vhostcfg += [ '', 'DocumentRoot  ' + @documentroot ]
+    end
+
+    if not @config.empty?
+	vhostcfg += [ '', @config ].flatten
+    end
 -%>
 <% if (httpd_version  and  httpd_version != ''  and
        scope.function_versioncmp([httpd_version, '2.3.11']) < 0)
@@ -71,24 +92,6 @@ NameVirtualHost <%= addr %>
 <% end -%>
 <VirtualHost <%= server_addrs.join(' ') %>>
 
-    ServerName <%= server_names[0] %>
-<% server_names[1..-1].each do |aliasname| -%>
-    ServerAlias <%= aliasname %>
-<% end -%>
-<% if @certname && @certname != ''
-    certdir = scope.lookupvar('x509certs::pki_certdir')
-    keydir  = scope.lookupvar('x509certs::pki_keydir')
--%>
-    SSLEngine on
-    SSLCertificateFile <%= certdir %>/<%= @certname %>-cert.pem
-    SSLCertificateKeyFile <%= keydir %>/<%= @certname %>-key.pem
-    SSLCertificateChainFile <%= certdir %>/<%= @certname %>-chain.pem
-
-<% end -%>
-<% if @documentroot && @documentroot != '' -%>
-    DocumentRoot  <%= @documentroot %>
-<% end -%>
-
-<%= [@config].flatten.join("\n").gsub(/^./, '    \&') %>
+<%= vhostcfg.join("\n").gsub(/^./, '    \&') %>
 
 </VirtualHost>
-- 
GitLab