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

convert from yml configuration to json

parent 8a81f90b
#!/usr/bin/env ruby
TEMPLATE = 'config/nginx.conf.erb'
BACKENDS = 'backends.yml'
CONFIG = 'config/nginx.conf'
require 'fileutils'
TEMPLATE = File.join(File.dirname(__FILE__), 'templates/nginx.conf.erb')
USER_CONFIG = 'static.json'
NGINX_CONFIG = 'config/nginx.conf'
require 'erb'
require 'yaml'
require 'json'
def port
ENV['PORT'] || 5000
end
def backends
YAML.load(File.read(BACKENDS))
rescue Errno::ENOENT
[]
class NginxConfig
json = {}
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
erb = ERB.new(File.read(TEMPLATE))
File.write(CONFIG, erb.result(binding))
erb = ERB.new(File.read(TEMPLATE))
klass = erb.def_class(NginxConfig, 'render()')
FileUtils.mkdir_p(File.dirname(NGINX_CONFIG))
File.write(NGINX_CONFIG, klass.new.render)
......@@ -33,10 +33,10 @@ http {
keepalive_timeout 5;
root public_html/;
<% backends.each do |backend| %>
location <%= backend['mount_point'] %> {
proxy_pass <%= backend['url'] %>;
<% proxies.each do |location, hash| %>
location <%= location %> {
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