Commit 4921ff51 authored by Terence Lee's avatar Terence Lee

convert from yml configuration to json

parent 8a81f90b
#!/usr/bin/env ruby #!/usr/bin/env ruby
TEMPLATE = 'config/nginx.conf.erb' require 'fileutils'
BACKENDS = 'backends.yml'
CONFIG = 'config/nginx.conf' TEMPLATE = File.join(File.dirname(__FILE__), 'templates/nginx.conf.erb')
USER_CONFIG = 'static.json'
NGINX_CONFIG = 'config/nginx.conf'
require 'erb' require 'erb'
require 'yaml' require 'json'
def port
ENV['PORT'] || 5000
end
def backends class NginxConfig
YAML.load(File.read(BACKENDS)) json = {}
rescue Errno::ENOENT json = JSON.parse(File.read(USER_CONFIG)) if File.exist?(USER_CONFIG)
[] json["port"] ||= ENV["PORT"] || 5000
json["proxies"] ||= {}
json.each do |key, value|
define_method(key) { value }
end
end end
erb = ERB.new(File.read(TEMPLATE)) erb = ERB.new(File.read(TEMPLATE))
klass = erb.def_class(NginxConfig, 'render()')
File.write(CONFIG, erb.result(binding)) FileUtils.mkdir_p(File.dirname(NGINX_CONFIG))
File.write(NGINX_CONFIG, klass.new.render)
...@@ -33,10 +33,10 @@ http { ...@@ -33,10 +33,10 @@ http {
keepalive_timeout 5; keepalive_timeout 5;
root public_html/; root public_html/;
<% backends.each do |backend| %> <% proxies.each do |location, hash| %>
location <%= backend['mount_point'] %> { location <%= location %> {
proxy_pass <%= backend['url'] %>; proxy_pass <%= hash['origin'] %>;
} }
<% end %> <% end %>
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment