Commit 6fe82621 authored by Terence Lee's avatar Terence Lee

custom routes

parent 19576666
......@@ -11,18 +11,44 @@ require 'json'
class NginxConfig
json = {}
json = JSON.parse(File.read(USER_CONFIG)) if File.exist?(USER_CONFIG)
json["port"] ||= ENV["PORT"] || 5000
json["root"] ||= "public_html/"
json["proxies"] ||= {}
json["clean_urls"] ||= false
json.each do |key, value|
define_method(key) { value }
def initialize(json_file)
json = {}
json = JSON.parse(File.read(json_file)) if File.exist?(json_file)
json["port"] ||= ENV["PORT"] || 5000
json["root"] ||= "public_html/"
json["proxies"] ||= {}
json["clean_urls"] ||= false
json["routes"] ||= {}
json["routes"] = Hash[json["routes"].map { |route, target| [to_regex(route), target] }]
json.each do |key, value|
self.class.send(:define_method, key) { value }
end
end
def context
binding
end
private
def to_regex(path)
segments = []
while !path.empty?
if path[0...2] == '**'
segments << '.*'
path = path[2..-1]
elsif path[0...1] == '*'
segments << '[^/]*'
path = path[1..-1]
else
next_star = path.index("*") || path.length
segments << Regexp.escape(path[0...next_star])
path = path[next_star..-1]
end
end
segments.join
end
end
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)
erb = ERB.new(File.read(TEMPLATE)).result(NginxConfig.new(USER_CONFIG).context)
File.write(NGINX_CONFIG, erb)
......@@ -42,6 +42,12 @@ http {
}
<% end %>
<% routes.each do |route, path| %>
location ~ ^<%= route %>$ {
alias <%= root %>/<%= path %>;
}
<% end %>
<% proxies.each do |location, hash| %>
location <%= location %> {
proxy_pass <%= hash['origin'] %>;
......
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