Skip to content

Commit

Permalink
feat: initial cherry-pick of facebook#218 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnorris authored Apr 25, 2023
2 parents 230c9dd + 98c18a2 commit b06054b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
56 changes: 37 additions & 19 deletions cookbooks/fb_apache/libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,55 @@ def self.indentstr(indent)
' ' * indent
end

# Map a hash to a apache-style syntax
def self.template_hash_handler(buf, indent, kw, data)
if HANDLERS.keys.include?(kw)
self.send(HANDLERS[kw], buf, indent, kw, data)
return
end
buf << indentstr(indent)
buf << "<#{kw}>\n"
data.each do |key, val|
def self.render_apache_conf(buf, depth, config)
config.each do |kw, val|
if HANDLERS.keys.include?(kw)
self.send(HANDLERS[kw], buf, depth, val)
next
end

indent = indentstr(depth)

case val
when String
buf << indentstr(indent + 1)
buf << "#{key} #{val}\n"
when String, Integer
buf << indent
buf << "#{kw} #{val}\n"

when Array
val.each do |entry|
buf << indent
buf << "#{kw} #{entry}\n"
end

when Hash
template_hash_handler(buf, indent + 1, key, val)
buf << indent
buf << "<#{kw}>\n"

render_apache_conf(buf, depth + 1, val)

buf << indent
buf << "</#{kw.split[0]}>\n"

else
fail "fb_apache: bad type for value of #{kw}: #{val.class}"
end
end
buf << indentstr(indent)
buf << "</#{kw.split[0]}>\n"
end

# Helper for rewrite syntax
def self.template_rewrite_helper(buf, _indent, _key, rules)
def self.template_rewrite_helper(buf, depth, rules)
indent = indentstr(depth)

rules.each do |name, ruleset|
buf << indentstr(1)
buf << indent
buf << "# #{name}\n"

ruleset['conditions']&.each do |cond|
buf << indentstr(1)
buf << indent
buf << "RewriteCond #{cond}\n"
end
buf << indentstr(1)

buf << indent
buf << "RewriteRule #{ruleset['rule']}\n\n"
end
end
Expand Down
14 changes: 0 additions & 14 deletions cookbooks/fb_apache/templates/default/apache_conf.erb

This file was deleted.

3 changes: 1 addition & 2 deletions cookbooks/fb_apache/templates/default/fb_apache.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
<%= render 'apache_modules.erb' %>

<% end %>
<%= render 'apache_conf.erb',
:variables => {:conf => node['fb_apache']['extra_configs']} %>
<% FB::Apache.render_apache_conf(_buf, 0, node['fb_apache']['extra_configs']) %>
2 changes: 1 addition & 1 deletion cookbooks/fb_apache/templates/default/fb_sites.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% realvhost = conf['_virtualhost'] || vhost %>
<VirtualHost <%= realvhost %>>
<% conf.reject! { |x, y| x == '_virtualhost' } %>
<%= render 'apache_conf.erb', :variables => {:conf => conf} %>
<% FB::Apache.render_apache_conf(_buf, 1, conf) %>
</VirtualHost>

<% end %>

0 comments on commit b06054b

Please sign in to comment.