Skip to content
Snippets Groups Projects
Commit 9ea4dcbf authored by Thomas Bellman's avatar Thomas Bellman
Browse files

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...
parent ef03095f
No related branches found
No related tags found
No related merge requests found
<%
# 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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment