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

custom routes

parent 19576666
...@@ -11,18 +11,44 @@ require 'json' ...@@ -11,18 +11,44 @@ require 'json'
class NginxConfig class NginxConfig
json = {} def initialize(json_file)
json = JSON.parse(File.read(USER_CONFIG)) if File.exist?(USER_CONFIG) json = {}
json["port"] ||= ENV["PORT"] || 5000 json = JSON.parse(File.read(json_file)) if File.exist?(json_file)
json["root"] ||= "public_html/" json["port"] ||= ENV["PORT"] || 5000
json["proxies"] ||= {} json["root"] ||= "public_html/"
json["clean_urls"] ||= false json["proxies"] ||= {}
json.each do |key, value| json["clean_urls"] ||= false
define_method(key) { value } 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
end end
erb = ERB.new(File.read(TEMPLATE))
klass = erb.def_class(NginxConfig, 'render()') erb = ERB.new(File.read(TEMPLATE)).result(NginxConfig.new(USER_CONFIG).context)
FileUtils.mkdir_p(File.dirname(NGINX_CONFIG)) File.write(NGINX_CONFIG, erb)
File.write(NGINX_CONFIG, klass.new.render)
...@@ -42,6 +42,12 @@ http { ...@@ -42,6 +42,12 @@ http {
} }
<% end %> <% end %>
<% routes.each do |route, path| %>
location ~ ^<%= route %>$ {
alias <%= root %>/<%= path %>;
}
<% end %>
<% proxies.each do |location, hash| %> <% proxies.each do |location, hash| %>
location <%= location %> { location <%= location %> {
proxy_pass <%= hash['origin'] %>; 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